LINQ to Entities 不支持 LINQ 表达式节点类型“Invoke”
今天使用使用动态查询的时候出现出现错误“LINQ to Entities 不支持 LINQ 表达式节点类型‘Invoke’。”,代码如下:
static
void
test()
{
ArticleEntities Entity = new ArticleEntities();
Expression < Func < Users, bool >> where = PredicateBuilder.True < Users > ()
.And(m => m.Name.Contains( " sanmen " ));
IEnumerable < Users > list = Entity.Users.Where( where );
foreach (Users user in list)
{
Console.WriteLine(user.ID + " " + user.Name);
}
{
ArticleEntities Entity = new ArticleEntities();
Expression < Func < Users, bool >> where = PredicateBuilder.True < Users > ()
.And(m => m.Name.Contains( " sanmen " ));
IEnumerable < Users > list = Entity.Users.Where( where );
foreach (Users user in list)
{
Console.WriteLine(user.ID + " " + user.Name);
}
}
研究了半天,发现他在Linq to sql 上运行正常,郁闷、费解了半天。找了一天终于找到解决方法即
where 后加.comple(),
代码:
static
void
test()
{
ArticleEntities Entity = new ArticleEntities();
Expression < Func < Users, bool >> where = PredicateBuilder.True < Users > ()
.And(m => m.Name.Contains( " sanmen " ));
IEnumerable < Users > list = Entity.Users.Where( where .Compile() );
foreach (Users user in list)
{
Console.WriteLine(user.ID + " " + user.Name);
}
{
ArticleEntities Entity = new ArticleEntities();
Expression < Func < Users, bool >> where = PredicateBuilder.True < Users > ()
.And(m => m.Name.Contains( " sanmen " ));
IEnumerable < Users > list = Entity.Users.Where( where .Compile() );
foreach (Users user in list)
{
Console.WriteLine(user.ID + " " + user.Name);
}
}
解决是解决了但是不知其原理,请教各位路过的大神解释解释。