【发布时间】:2013-09-13 14:27:34
【问题描述】:
我想运行这个 LINQ 简单代码以在 LINQ 中记录编号,但结果低于错误
var model = _db2.Persons.Select(
(x, index) => new
{
rn = index + 1,
col1 = x.Id
}).ToList();
错误:
LINQ to Entities 无法识别该方法 'System.Linq.IQueryable
1[<>f__AnonymousType22 [System.Int32,System.Int32]] 选择[Person,f__AnonymousType22](System.Linq.IQueryable1 [MvcApplication27.Models.Person],System.Linq.Expressions.Expression1[System.Func3 [MvcApplication27.Models.Person,System.Int32,f__AnonymousType2`2 [System.Int32,System.Int32]]])' 方法,并且这个方法不能翻译成store 表达。
【问题讨论】:
-
看起来
Persons不是IQueryable,请先尝试使用Cast或OfType(当然如果它们存在的话)。 -
不,这是因为索引查找记录号。这是代码优先,当我在 lambda 中删除索引时我没有问题!
-
作为标题的注释
System.Linq.IQueryable是返回类型,而不是方法名称。您在方括号 ([<>f__AnonymousType22 [System.Int32,System.Int32]]) 中看到的内容是<int, f__AnonymousType22<>>中的内容,如果您在哪里输入的话。实际功能在该部分之后Select[Person,<>f_AnonymousType22](...)。
标签: c# .net linq entity-framework