如何使用C#汇总重复记录和总和值[英] How to aggregate duplicate records and sum value using C#

本文是小编为大家收集整理的关于如何使用C#汇总重复记录和总和值的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想创建一个列表,其中包含多个代理商以及他们拨打的电话数量和这样做:

public class Agent
{
    public string Agent_ID{ get; set; }
    public string Name { get; set; }
    public int Calls { get; set; }

}

var list = new List<Agent>() // To create a list to hold the data
{ 
    new Agent() { Agent_ID = "TK_J", Name = "James", Calls = 10 },
    new Agent() { Agent_ID = "TK_K", Name = "Kurtis", Calls = 10 },
    new Agent() { Agent_ID = "TK_R", Name = "Rebecca", Calls = 5 },
    new Agent() { Agent_ID = "TK_J", Name = "James", Calls = 10 },
    new Agent() { Agent_ID = "TK_R", Name = "Rebecca", Calls = 5 },
    new Agents(){ Agent_ID = "TK_B", Name = "Bobby", Calls = 10 },
};

您可以看到,将有冗余数据行.因此,我想使用C#聚合功能(例如Group)来总结类似的代理的呼叫数量.我正在尝试的是:

list.GroupBy(i => i.Agent_ID).Select(g => new 
{ 
Agent_ID= g.Key, 
Name = */ How do i bring the name here*/, 
Calls = g.Sum(i => i.Calls)});

有人可以帮助我吗?感谢任何帮助或建议.如果有问题,请教我如何修复代码.非常感谢!

推荐答案

您当前仅由AgentID进行分组. 您需要将所需的字段投影为匿名对象,以便它们可以作为IGrouping<annonymous,Agent>提供给选择. 请参阅下面.

list.GroupBy(i => new {i.Agent_ID, i.Name}).Select(g => new 
{ 
Agent_ID= g.Key.Agent_ID, 
Name = g.Key.Name, 
Calls = g.Sum(i => i.Calls)
});

其他推荐答案

假设Agent_ID将与代理的Name属性同步,您也可以使用像First()这样的汇总来返回组中的任何Agent记录的名称,因为所有代理都在每个组中,都有相同的名称:

list.GroupBy(i => i.Agent_ID)
.Select(g => new Agent // Strong class
{ 
   Agent_ID= g.Key.Agent_ID,
   Name = g.First().Name,
   Calls = g.Sum(i => i.Calls)
})

另外,由于您似乎正在重新投射到相同的形状,所以为什么不保留并投射到强大的班级,而不是新的anon类呢?

本文地址:https://www.itbaoku.cn/post/1556813.html

问题描述

I want to create a list where it holds several agents and the number of calls they make and did it like so:

public class Agent
{
    public string Agent_ID{ get; set; }
    public string Name { get; set; }
    public int Calls { get; set; }

}

var list = new List<Agent>() // To create a list to hold the data
{ 
    new Agent() { Agent_ID = "TK_J", Name = "James", Calls = 10 },
    new Agent() { Agent_ID = "TK_K", Name = "Kurtis", Calls = 10 },
    new Agent() { Agent_ID = "TK_R", Name = "Rebecca", Calls = 5 },
    new Agent() { Agent_ID = "TK_J", Name = "James", Calls = 10 },
    new Agent() { Agent_ID = "TK_R", Name = "Rebecca", Calls = 5 },
    new Agents(){ Agent_ID = "TK_B", Name = "Bobby", Calls = 10 },
};

As you can see there will be redundant lines of data. So I want to use C# aggregation function such as group by to sum up the similar agent's number of calls. What I was trying was:

list.GroupBy(i => i.Agent_ID).Select(g => new 
{ 
Agent_ID= g.Key, 
Name = */ How do i bring the name here*/, 
Calls = g.Sum(i => i.Calls)});

Anyone can help me? Appreciate any help or advice. If there is something wrong teach me how to fix the code. Many thanks!!

推荐答案

You are currently grouping by only AgentID. You'll need to project the fields you require as an anonymous object so they can be available as an IGrouping<annonymous,Agent> to the select. See below.

list.GroupBy(i => new {i.Agent_ID, i.Name}).Select(g => new 
{ 
Agent_ID= g.Key.Agent_ID, 
Name = g.Key.Name, 
Calls = g.Sum(i => i.Calls)
});

其他推荐答案

Assuming that Agent_ID will be synchronized with the Agent's Name property, you can also use an aggregate like First() to return the the name from any Agent record in the group, since all agents in each group will have the same name:

list.GroupBy(i => i.Agent_ID)
.Select(g => new Agent // Strong class
{ 
   Agent_ID= g.Key.Agent_ID,
   Name = g.First().Name,
   Calls = g.Sum(i => i.Calls)
})

Also, since you seem to be projecting back into the same shape, why not retain and project into the strong class, rather than a new anon class?