Linq2Sql 和 lambda 返回“方法‘System.Object DynamicInvoke(System.Object[])’”不支持 SQL 转换”
为什么...
Func<IQueryable<CampaignCodePaths>> table = () => CampaignCodePaths;
Func<CampaignCodePaths, int> propertySelector = e => e.Id;
int key = 1;
Func<CampaignCodePaths, bool> whereSelector = e => propertySelector(e).Equals(key);
table().Where(whereSelector).FirstOrDefault();
...有效但是...
Func<IQueryable<CampaignCodePaths>> table = () => CampaignCodePaths;
Func<CampaignCodePaths, int> propertySelector = e => e.Id;
int key = 1;
table().Where(e => propertySelector(e).Equals(key)).FirstOrDefault();
...返回异常:
方法“System.Object DynamicInvoke(System.Object[])”不支持对 SQL 的转换
?
更新
澄清:
CampaignCodePath Get(Func<IQueryable<CampaignCodePaths>> table, Func<CampaignCodePaths, int> selector, int key)
{
return table().Where(/*How to I create this expression from selector and key? */).FirstOrDefault();
}
...
Get(() => CampaignCodePaths, e => e.Id, 1)
Why does...
Func<IQueryable<CampaignCodePaths>> table = () => CampaignCodePaths;
Func<CampaignCodePaths, int> propertySelector = e => e.Id;
int key = 1;
Func<CampaignCodePaths, bool> whereSelector = e => propertySelector(e).Equals(key);
table().Where(whereSelector).FirstOrDefault();
...work but...
Func<IQueryable<CampaignCodePaths>> table = () => CampaignCodePaths;
Func<CampaignCodePaths, int> propertySelector = e => e.Id;
int key = 1;
table().Where(e => propertySelector(e).Equals(key)).FirstOrDefault();
...returns exception:
Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL
?
UPDATE
To clarify:
CampaignCodePath Get(Func<IQueryable<CampaignCodePaths>> table, Func<CampaignCodePaths, int> selector, int key)
{
return table().Where(/*How to I create this expression from selector and key? */).FirstOrDefault();
}
...
Get(() => CampaignCodePaths, e => e.Id, 1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的第一段代码是在 .NET 中执行所有过滤,因为您使用的是委托而不是表达式树 - 它甚至没有尝试将过滤器转换为 SQL。换句话说,并不是第一个“有效”而第二个不行——而是第一个不会失败,因为它并没有真正尝试做你所期望的事情,而第二个却成功了。
第二种形式是调用 Queryable.Where(IQueryable, Expression<...>),而第一种形式是调用
Enumerable.Where(IEnumerable, Func<.. .>)
。如果你将代码更改为:
那么应该没问题。
编辑:回应您的编辑,我认为您想要类似的东西:
Your first piece of code is performing all the filtering in .NET because you're using a delegate instead of an expression tree - it's not even trying to convert the filter into SQL. In other words, it's not that the first "works" and the second doesn't - it's that the first doesn't fail because it doesn't really try to do what you're expecting, whereas the second does.
The second form is calling
Queryable.Where(IQueryable<T>, Expression<...>)
whereas the first is callingEnumerable.Where(IEnumerable<T>, Func<...>)
.If you change your code to:
then it should be fine.
EDIT: Responding to your edit, I think you want something like: