Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq到SQL:选择多列求和_C#_.net_Linq_Linq To Sql - Fatal编程技术网

C# Linq到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

我想将以下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
               {
                     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语句无效