gpt4 book ai didi

linq-to-entities - SkipWhile 失败并显示 "LINQ to Entities does not recognize the method ..."

转载 作者:行者123 更新时间:2023-12-04 08:35:12 32 4
gpt4 key购买 nike

我找不到为什么会发生以下异常。非常感谢任何帮助。

// 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.IQueryable
1[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 ),因此使用这样的谓词是没有意义的,因此它们不存在。

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

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

当然,你可能想做这样的事情:
query = query.OrderBy(x => x.CollegeId).Where(x => x.CollegeID > 100);

关于linq-to-entities - SkipWhile 失败并显示 "LINQ to Entities does not recognize the method ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4412233/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com