C# Linq到SQL:选择多列求和
我想将以下SQL代码转换为linq to SQL,但似乎找不到方法C# Linq到SQL:选择多列求和,c#,.net,linq,linq-to-sql,C#,.net,Linq,Linq To Sql,我想将以下SQL代码转换为linq to 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
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),
}
);
如何选择其他两列?我遗漏了什么?这是您的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上下文,并且其中有您的表。事实上,您的SQL查询
仅在holder\u name
和agent\u code
之间的对应关系为1-1
时才起作用,否则按agent\u code>分组
将不起作用。因此,您的linq查询应如下所示:
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 to sql的.net代码,为什么它属于服务器故障?从ctx.agent\u代码组a中按a.holder\u name,a.code进入总计选择{holder\u name=a.holder\u name,code=a.code,total=totals}
@vittor很抱歉标签中的输入错误…@vittor我不确定你的代码是什么。。。我似乎看不到求和的部分…对不起,它不能解决我的问题。我只需要按代理人代码分组,但显示持有人姓名、代理人代码和持有人姓名来源的金额(合计)?若未将持有者名称和代码一起放入组中,则原始sql语句无效