Grouping over a list of dictionaries I am consuming data from an API that reads from a SharePoint list. The API is basically this:

IList&lt;Dictionary&lt;string,string&gt;&gt; GetListData(string listName);


So the data is a list of dictionaries - each dictionary containing value-pairs (column name, column value) for a "row" in the list.

I need to group over the results, doing a sum over one column.

I have made code that does exactly that - but I am not entirely happy with it.

The code is this:

[Test]
public void TestOfSharePointResult()
{
    //Arrange
    IList&lt;Dictionary&lt;string, string&gt;&gt; sharePointResult = new List&lt;Dictionary&lt;string, string&gt;&gt;
    {
        new Dictionary&lt;string, string&gt; { {"TO", "101"}, { "PA", "123" } },
        new Dictionary&lt;string, string&gt; { {"TO", "202"}, { "PA", "456" } },
        new Dictionary&lt;string, string&gt; { {"TO", "404"}, { "PA", "123" } }
    };

    //Act
    var valuePairs = sharePointResult
        .Select(dict =&gt; new KeyValuePair&lt;string, int&gt;(dict["PA"], dict["TO"].FromProjectValueToInt()))
        .GroupBy(d =&gt; d.Key)
        .Select(g =&gt; new { PA = g.Key, Sum = g.Sum(f =&gt; f.Value) })
        ;

    //Assert

    // Result must be:
    //  PA  Sum
    //  --------
    //  123 505
    //  456 202
    var TOValueFor123 = valuePairs.First(i =&gt; i.PA == "123").Sum;
    var TOValueFor456 = valuePairs.First(i =&gt; i.PA == "456").Sum;
    Assert.AreEqual(505, TOValueFor123);
    Assert.AreEqual(202, TOValueFor456);
}


The "FromProjectValueToInt()" extension basically converts a string to an int.

The reason I am not entirely happy with this code is line 14, where I am doing an explicit extraction of values from the dictionary entries to a KeyValuePair struct.

Is there a more implicit way of looping over the list of dictionaries that I am missing?
You don't need neither Select and can get the same result by using only GroupBy:

var valuePairs = sharePointResult
    .GroupBy(
        dict =&gt; dict["PA"], 
        dict =&gt; int.Parse(dict["TO"]), 
        (key, items) =&gt; new
        {
            PA = key,
            Sum = items.Sum(f =&gt; f)
        });


where the parameters are respectively:

.GroupBy(keySelector, elementSelector, resultSelector);


I used int.Parse for testing - it's not a part of my review ;-)