相关疑难解决方法(0)

为什么EF抛出"NotSupportedException:方法'First'只能用作最终查询操作"

int[] ids1 = { 1, 2, 3 };
int[] ids2 = { 1, 5, 6 };

var result = from a in ids1
             where a == ids2.First()
             select a;  
foreach (var item in result) ; //ok


var employees = from c in context.Employees.
                where c.EmployeeID == ids1.First() 
                select c;   
foreach (var item in employees); // NotSupportedException
Run Code Online (Sandbox Code Playgroud)

当尝试ids1.First在Linq-to-Entities查询中调用时,我得到一个异常 System.NotSupportedException:方法'First'只能用作最终查询操作.请考虑在此实例中使用方法"FirstOrDefault".

a)我不明白为什么First只能用作最终查询操作,因为在我们的例子First中调用on IEnumerable<>(ids1.First())而不是on IQueryable<>.换句话说,First在Linq-to-Objects查询中调用而不是在Linq-to-Entities查询中调用?!

b)无论如何,为什么必须First用作最终查询操作,而FirstOrDefault不必进行最终查询操作?

谢谢 …

linq-to-objects linq-to-entities

7
推荐指数
1
解决办法
4806
查看次数

标签 统计

linq-to-entities ×1

linq-to-objects ×1