I have this List<object[]>
List<object[]> olst = new List<object[]>();
olst.Add(new object[] { "AA1", 1 });
olst.Add(new object[] { "AA2", 1 });
olst.Add(new object[] { "AA2", 1 });
olst.Add(new object[] { "AA1", 1 });
olst.Add(new object[] { "AA1", 1 });
From olst
, I need to produce a new List<object>
to hold this:
"AA1", 3
"AA2", 2
In other words, I need to group olst[x][0] and sum up olst[x][1].
I could use a for loop, but I was hoping someone could help me using lambda expressions and/or linq to accomplish this.
Use
GroupBy
andSelect
:This will turn you list into a dictionary mapping from the first value to the sum of the second values with the same first value.
I missed the part stating the the result should be of type
List<Object>
containingObject[]
. It would help if the downvoters would leave a comment about the why. ;)