LINQ to Entities 不识别方法“XX”,因此该方法无法转换为存储表达式。
总结了下,出现这种错误情形大体分为两种,下边分别举例并说下解决方法。
情形一
报错代码:
string age = "20";
var query = from item in context.People
where item.Age == int.Parse(age)
select item;这边age是一个外部值,试图在LINQ to Entities进行类型转化,不支持,所以报错,只需在LINQ to Entities前先转化类型即可解决:
int age = int.Parse("20");
var query = from item in context.People
where item.Age == age
select item;或是使用表达式树:
string age = "20";
Expression<Func<Person, bool>> exp = item => item.Age == int.Parse(age);
var query = context.People.Where(exp.Compile());情形二
报错代码:
var query = from item in context.People
group item by item.Created.ToShortDateString() into g
select new { Created = g.Key, Count = g.Count() };这边要转化的是字段值,因而无法像情形一那样在外部先处理,解决方法是将数据先从数据库读取出来,然后在内存中使用LINQ to Objects解决:
var query = from item in context.People[color=Red].ToList()[/color]
group item by item.Created.ToShortDateString() into g
select new { Created = g.Key, Count = g.Count() };
评论(0)
暂无评论。