我在 C# 中有一个列表,例如
A,1
B,2
C,3
A,4
B,5
我想以列表形式返回结果:
A,5
B,7
C,3
所以按第一列分组并对第二列求和。
我该怎么做呢?很高兴使用 LINQ。到目前为止的声明和代码如下:
class Program
{
static void Main(string[] args)
{
List<MyList> list = new List<MyList>();
list.Add(new MyList() { Letter = "A", Value = 1 });
list.Add(new MyList() { Letter = "B", Value = 2 });
etc...
}
}
class MyList
{
public string Letter { get; set; }
public long Value { get; set; }
}