SkipWhile失败,并显示"LINQ to Entities无法识别该方法...". [英] SkipWhile fails with "LINQ to Entities does not recognize the method ..."

查看:58
本文介绍了SkipWhile失败,并显示"LINQ to Entities无法识别该方法...".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到为什么发生以下异常.任何帮助深表感谢.

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

例外:

LINQ to Entities无法识别该方法 'System.Linq.IQueryable 1[EDCS.ServiceLayer.DataAccess.College] SkipWhile[College](System.Linq.IQueryable 1 [EDCS.ServiceLayer.DataAccess.College], System.Linq.Expressions.Expression 1[System.Func 2 [EDCS.ServiceLayer.DataAccess.College,System.Boolean]])' 方法,并且该方法不能转换为商店表达式.

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.

推荐答案

您不能将SkipWhile与EF一起使用,因为没有很好的方法将它们转换为SQL.由于SQL查询返回无序集(除非您使用ORDER BY),所以使用这样的谓词没有意义,因此它们不存在.

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.

在EF中使用SkipWhile的方法是在调用它之前将查询转换为带有AsEnumerable()的对象:

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

这篇关于SkipWhile失败,并显示"LINQ to Entities无法识别该方法...".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆