Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 453
Laura Avatar asked Feb 25 '23 14:02

Laura


1 Answers

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);
like image 136
Gabe Avatar answered Feb 28 '23 18:02

Gabe