报错:System.NotSupportedException: LINQ to Entities does not recognize the method

时间:2023-03-08 16:38:50

报错:System.NotSupportedException: LINQ to Entities does not recognize the method ...... get_Item(Int32)' method, and this method cannot be translated into a store expression.

在控制器中有如下一段代码:

var tempList = SomeService.LoadEntities(c => c.DelFlag == (short)DelFlagEnum.Normal);

var result = from l in tempList
             select new { n = l.Name, a = l.Age };

LoadEntities方法返回的类型是IQueryable<T>,如果要对集合用Select方法,在用Select方法之前需要ToList一下。

修改成:

var result = from l in tempList.ToList()
             select new { n = l.Name, a = l.Age };