开发者

Using a predicate to find all rows?

开发者 https://www.devze.com 2023-04-13 06:19 出处:网络
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:

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号