views:

79

answers:

2

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.

+5  A: 

That's pretty simple:

int sum = dictionary.Sum(x => x.Value.Count);
Jon Skeet
A: 

You can implement a count method which loops through the dictionary and adds the count of internal lists.

Better approach would be to create a specialized collection and maintain this data structure internally. You can override the count method in that class to return count. Then you can optimize by keeping a class level counter and update it while adding and removing items.

Unmesh Kondolikar