Jon Jon - 1 month ago 7
C# Question

Calculating Total Count of Items in List<T> for multiple KeyValuePairs in Dictionary<string,List<string>>

I have a

Dictionary<int,List<string>>
.

I have multiple KeyValuePairs so int the first KeyValuePair the List may have 10 items in it, the second KeyValuePair may have 100 etc.

I need to work out the total count for all items in each list so in my example above I would have a result of 110.

Answer

That's pretty simple:

int sum = dictionary.Sum(x => x.Value.Count);