开发者

SkipWhile fails with "LINQ to Entities does not recognize the method ..."

I cannot find why the following exception occurs. Any help is most appreciated.

// EdcsEntities is derived from System.Data.Objects.ObjectContext
EdcsEntities db = new EdcsEntities();

var query = from i in db.Colleges
            select i;

query = query.SkipWhile<College>(x => x.CollegeID != 100);

List<College> l = query.ToList<College>()开发者_JAVA百科;

Exception:

LINQ to Entities does not recognize the method 'System.Linq.IQueryable1[EDCS.ServiceLayer.DataAccess.College] SkipWhile[College](System.Linq.IQueryable1[EDCS.ServiceLayer.DataAccess.College], System.Linq.Expressions.Expression1[System.Func2[EDCS.ServiceLayer.DataAccess.College, System.Boolean]])' method, and this method cannot be translated into a store expression.


You can't use SkipWhile with EF because there's no good way to translate them to SQL. Since SQL queries return unordered sets (unless you use ORDER BY) it doesn't make sense to use predicates like that, so they don't exist.

The way to use SkipWhile in EF is to just turn the query into objects with AsEnumerable() before calling it:

query = query.AsEnumerable().SkipWhile(x => x.CollegeID != 100);

Of course you probably want to do something like this:

query = query.OrderBy(x => x.CollegeId).Where(x => x.CollegeID > 100);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜