繁体   English   中英

Linq to SQL:选择多列的Sum

[英]Linq to SQL: Sum with multiple columns select

我想将以下SQL代码转换为linq到sql但似乎无法找到方法

select holder_name,agent_code,sum(total) 
from agent_commission
group by agent_code

谁能帮我? 有点坚持这一点。

提前致谢

更新:我尝试了以下内容

var query = (from p in context.Agent_Commissions
               group p by new
               {
                     p.agent_code
               }
               into s
               select new
               {
                    amount = s.Sum(q => q.total),
                }
              );

如何选择其他两列? 我错过了什么?

实际上, 只有holder_nameagent_code之间的对应关系为1-1 ,您的SQL query 才有效 ,否则Group by agent_code将无效。 所以你的linq query应该是这样的:

var query =  from p in context.Agent_Commissions
             group p by p.agent_code into s
             select new {
                holder_name = s.FirstOrDefault().holder_name,
                agent_code = s.Key,
                amount = s.Sum(q => q.total)
             };

这是你的linq查询

from a in ctx.agent_code 
group a by a.holder_name, a.code into totals 
select { holder_name = a.holder_name, 
         code = a.code, 
         total = totals.Sum(t=>t.total)} 

鉴于你在ctx变量中有linq2sql上下文并且它有你的表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM