2011-09-08 105 views
0

在Linq中不允许算术运算符。它给错误+不能应用于lambda表达式。 CostperResponse.Cost是数据类型小数的linq中的算术运算符

 var costperrespone= from v in lstSale 
           group v by v.ActionGroupName into g 
           select new CostperResponse 
           { 
            ActionGroupName = g.Key, 
            Cost = ((x => Convert.ToDecimal(x.ResponseROCount)))/((x => Convert.ToDecimal(x.ResponseWPAmount)) + (x => Convert.ToDecimal(x.ResponseWPAmount))) 
           }; 

SQLServer的查询是

 select SUM((cast(isnull(response_ro_count,0) as decimal))/(cast(isnull(response_cp_amount,0)as decimal) + cast(isnull(response_wp_amount,0)as decimal))) , action_group_name from GM_Tempdata where cast(response_ro_count as decimal)>0 group by action_group_name 
+0

你正在尝试添加两个lambda表达式,这可能不是你在做什么之后? – lbergnehr

+1

'x'从哪里来? – Jon

+0

你必须引入变量'let'operator –

回答

0
Cost = ((x => Convert.ToDecimal(x.ResponseROCount)))/((x => Convert.ToDecimal(x.ResponseWPAmount)) + (x => Convert.ToDecimal(x.ResponseWPAmount))) 

“的意思是”

Cost = (somefunc(x) {Convert.ToDecimal(x.ResponseROCount)))} 
/(anotherFunc(x) { Convert.ToDecimal(x.ResponseWPAmount))} 
+ lastFunc(x) {Convert.ToDecimal(x.ResponseWPAmount)})) 

我不太清楚你要分配给什么成本?

也许你的意思是像

Cost = Convert.ToDecimal(v.ResponseROCount)/Convert.ToDecimal(v.ResponseWPAmount) + Convert.ToDecimal(v.ResponseWPAmount) 

但既然你已经分组,你将需要使用像总和()等的集合体?

+0

明确的sqlserver查询是。选择 \t SUM((铸造(ISNULL(response_ro_count,0)为十进制))/(铸造(ISNULL(response_cp_amount,0)为十进制)+铸造(ISNULL(response_wp_amount,0)为十进制))), \t action_group_name 从 \t GM_Tempdata 其中 \t铸造(response_ro_count为十进制)> 0 组由 \t action_group_name – Chakradhar