LINQ to Entities 不支持 LINQ 表达式节点类型“Invoke”

本文介绍了解决使用LINQtoEntities时遇到的“Invoke”类型不支持的问题,通过在表达式后添加.Compile()方法,成功实现了动态查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
            }

        }

研究了半天,发现他在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);
            }

}

解决是解决了但是不知其原理,请教各位路过的大神解释解释。

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值