Laura
Laura

Reputation: 71

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.

Upvotes: 7

Views: 3360

Answers (1)

Gabe
Gabe

Reputation: 86768

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

Upvotes: 9

Related Questions