Entity Framework Code First IQueryable

ε祈祈猫儿з 提交于 2019-12-01 17:09:49

Because you are querying an IEnumerable (ie: LINQ to Objects) not IQueryable (ie: LINQ to Entities) given by EF.

Instead you should use

IEnumerable<History> results = context.History.Where(h => h.Person.Id = "sfssd").OrderBy(h => h.OnDate).Take(50)

This question and the accepted answer are both a bit old. Code like this would, as the original question points, load the entire history for the person from the database - not good!

var results = person
    .History
    .OrderBy(h => h.OnDate)
    .Take(50)
    .ToArray();

With EF 6 there is an easy solution. Without rearranging your query, you can have it work the IQueryable way by making use of the DbContext.Entry method, the DbEntryEntity.Collection method, and the DbCollectionEntry.Query method.

var results = dbContext
    .Entry(person)
    .Collection(p => p.History)
    .Query()
    .OrderBy(h => h.OnDate)
    .Take(50)
    .ToArray();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!