如何解决问题“无法将表达式...转换为SQL,并且不能将其视为本地表达式." [英] How to solve issue "Could not translate expression ...into SQL and could not treat it as a local expression."
本文介绍了如何解决问题“无法将表达式...转换为SQL,并且不能将其视为本地表达式."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下:
void Main()
{
var q = from a in Applicants
where(a.Claims.Any())
select a.Claims.Sum(c => c.TotalClaimAmount());
q.Dump();
}
public static class MyExt
{
public static decimal TotalClaimAmount(this Claim c)
{
var t = c.Accommodations.Sum(a => a.AmountClaimed) +
c.MealAllowances.Sum(ma => ma.AmountClaimed) +
c.Meals.Sum(m => m.AmountClaimed) +
c.Mileages.Sum(mi => mi.AmountClaimed) +
c.Others.Sum(o => o.AmountClaimed) +
c.ParkingTransits.Sum(pt => pt.AmountClaimed) +
c.Travels.Sum(tr => tr.AmountClaimed);
return (decimal)t;
}
}
当我在LinqPad中运行它时,出现以下问题:
when I run it in LinqPad get below issue:
InvalidOperationException:无法将表达式'a.Claims.Sum(c => c.TotalClaimAmount())'转换为SQL,因此无法将其视为本地表达式.
InvalidOperationException: Could not translate expression 'a.Claims.Sum(c => c.TotalClaimAmount())' into SQL and could not treat it as a local expression.
请帮帮我.非常感谢
推荐答案
在SQL中,您的自定义方法没有相应的命令.因此编译器无法翻译您的表达式.取而代之的是先获取您的物品,然后选择结果:
There is no corresponding command for your custom method in SQL. so compiler could not translate your expression. Instead first get your items,then select your results:
var items = Applicants.Where(a => a.Claims.Any()).ToList();
var results = items.Select(a => a.Claims.Sum(c => c.TotalClaimAmount());
这篇关于如何解决问题“无法将表达式...转换为SQL,并且不能将其视为本地表达式."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文