LINQ 表达式节点类型“Invoke” LINQ to Entities 不支持
我尝试通过 lambda 表达式在 EF 查询中实现“IN”子句,如下所示:
/*lambda expression to evaluate: status in[a, b, c...n]*/
_IN myIN = (allStatus, status) =>
{
bool lambdaResult = false;
foreach (int s in statuses)
{
if (status == s)
{
lambdaResult = true;
break;
}
}
return lambdaResult;
};
result = (from v in _entity.CAVouchers
join vs in _entity.CAVoucherStatus.Where(vs => (myIN(statuses, vs.VoucherStatusId) == true)) on v.VoucherStatusId equals vs.VoucherStatusId
join vsr in _entity.CAVoucherStatusReason on v.VoucherStatusReasonId equals vsr.VoucherStatusReasonid
where v.CyberAgent.CAID == caid
select new ACPVoucher()
{
...
}).ToList();
由于“where”,这将引发“LINQ to Entities 中不支持 LINQ 表达式节点类型“Invoke””错误健康)状况。 基本上我需要的是“where statusId in [1, 2, 3]”类型的子句。
失败是什么?...有关如何执行此操作的任何提示?当应用于通用列表时,同样的方法对我也有效。
谢谢
I'm trying to implement a 'IN' clause in my EF query through a lambda expression as follows:
/*lambda expression to evaluate: status in[a, b, c...n]*/
_IN myIN = (allStatus, status) =>
{
bool lambdaResult = false;
foreach (int s in statuses)
{
if (status == s)
{
lambdaResult = true;
break;
}
}
return lambdaResult;
};
result = (from v in _entity.CAVouchers
join vs in _entity.CAVoucherStatus.Where(vs => (myIN(statuses, vs.VoucherStatusId) == true)) on v.VoucherStatusId equals vs.VoucherStatusId
join vsr in _entity.CAVoucherStatusReason on v.VoucherStatusReasonId equals vsr.VoucherStatusReasonid
where v.CyberAgent.CAID == caid
select new ACPVoucher()
{
...
}).ToList();
This is throwing a "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities" error due to the "where" condition.
Basically what I need is a "where statusId in [1, 2, 3]" kind of clause.
What is failing?...any tips on how to do this? The same approach is working for me when applied to a generic list.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以只做一个包含查询:
You could just do a contains query:
你能试试这个吗?
Can you try this?
不同的 LINQ 提供者支持不同的技巧;碰巧 LINQ-to-SQL 可以与调用一起使用。 EF:没那么多。如果您手动构建表达式,则可能需要在不调用的情况下形成表达式,或者使用重写器来展平它。这里有一个重写器示例说明了如何执行此操作: 组合两个 lambda 表达式在 C# 中
Different LINQ providers support different tricks; as it happens LINQ-to-SQL works with invoke. EF: not so much. If you are building the expression by hand, you may need to either form the expression without invoke, or to use a re-writer to flatten it. There's a rewriter example here that illustrates how to do this: Combining two lambda expressions in c#