This question is locked. New answers and comments are not allowed.
Hi Guys!
I am currently having problems on executing a LINQ expression against a repository.
Find below my IRepository, Repository and Controller.
IRepository Class
Repository Class
HomeController
The commented line on HomeController is working fine. but I am actually not using predicate.
How would I properly execute "Pega" for getting the same result?
Thanks,
Leandro Andrade
I am currently having problems on executing a LINQ expression against a repository.
Find below my IRepository, Repository and Controller.
IRepository Class
public
interface
IRepositorio<T> where T :
class
{
IList<T> PegaTudo();
IList<T> PegaTudoLimite(
int
qtd);
T Pega(Expression<Func<T,
bool
>> predicate);
void
Inserir(T item);
void
Remover(T item);
void
Salvar();
}
Repository Class
public
class
Repositorio<T> : IDisposable, IRepositorio<T> where T :
class
{
protected
EmpregaDB db {
get
;
private
set
;
}
public
Repositorio(EmpregaDB _db) {
this
.db = _db;
}
public
virtual
IList<T> PegaTudo() {
return
this
.db.GetAll<T>().ToList();
}
public
virtual
IList<T> PegaTudoLimite(
int
qtd) {
return
this
.db.GetAll<T>().Take(qtd).ToList();
}
public
virtual
T Pega(Expression<Func<T,
bool
>> predicate) {
return
this
.db.GetAll<T>().FirstOrDefault(predicate);
}
public
virtual
void
Inserir(T item) {
this
.db.Add(item);
}
public
virtual
void
Remover(T item) {
this
.db.Delete(item);
}
public
virtual
void
Salvar() {
this
.db.SaveChanges();
}
public
void
Dispose() {
this
.db =
null
;
}
}
HomeController
public
class
HomeController : Controller
{
private
readonly
IRepositorio<Estado> repo;
public
HomeController() {
this
.repo =
new
Repositorio<Estado>(
new
EmpregaDB());
}
public
HomeController(IRepositorio<Estado> _repo) {
this
.repo = _repo;
}
public
ActionResult Index()
{
ViewBag.Message =
"Welcome to ASP.NET MVC!"
;
return
View();
}
public
ActionResult About()
{
return
View();
}
public
JsonResult PegaEstados() {
//var lista = repo.PegaTudo().Select(s => new { s.EstadoId, s.DscEstado }).Where( i => i.EstadoId == 1);
var lista = repo.Pega( i => i.EstadoId == 1 );
return
Json(lista, JsonRequestBehavior.AllowGet);
}
}
How would I properly execute "Pega" for getting the same result?
Thanks,
Leandro Andrade