Using a predicate to find all rows?
I have the following code, I am using the repository pattern in EF 4.1 and Unit of Work. However because I dont understand very much how Expression and Predicates works I ask the following:
With the code below, is there a better way to find all rows?
public ActionResult Index()
{
var positions = unitOfWork.PositionRepository
.Find(p => p.PositionID != null);
return View(positions.ToList());
}
I based my UnitofWork and Repository from here http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
I tried this:
public virtual List<TEntity> GetAll()
{
retur开发者_如何学Gon context.Set<TEntity>.ToList();
}
If you want all rows you simply have to call this on your set:
context.Positions.ToList();
So simply add method to your repository exposing this result.
In case of generic (wrong) repository use this:
context.Set<Position>().ToList();
精彩评论