I have a list in C# e.g.
A,1
B,2
C,3
A,4
B,5
I want to return the result in a list as:
A,5
B,7
C,3
So grouping by the first column and summing the second.
How do I do this? Happy to use LINQ. Declaration and code so far as follows:
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; }
}
Sounds like you want:
(It’s not clear what your types are, but hopefully you’ll get the needed list from this.)
There’s an overload of
GroupBywhich will let you do all of this in one call, but I personally find the above version simpler.