I am consuming data from an API that reads from a SharePoint list. The API is basically this:
IList<Dictionary<string,string>> 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<Dictionary<string, string>> sharePointResult = new List<Dictionary<string, string>>
{
new Dictionary<string, string> { {"TO", "101"}, { "PA", "123" } },
new Dictionary<string, string> { {"TO", "202"}, { "PA", "456" } },
new Dictionary<string, string> { {"TO", "404"}, { "PA", "123" } }
};
//Act
var valuePairs = sharePointResult
.Select(dict => new KeyValuePair<string, int>(dict["PA"], dict["TO"].FromProjectValueToInt()))
.GroupBy(d => d.Key)
.Select(g => new { PA = g.Key, Sum = g.Sum(f => f.Value) })
;
//Assert
// Result must be:
// PA Sum
// --------
// 123 505
// 456 202
var TOValueFor123 = valuePairs.First(i => i.PA == "123").Sum;
var TOValueFor456 = valuePairs.First(i => 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?
string
! \$\endgroup\$