views:

466

answers:

4

I want to get the total number of items in the Lists in the following Dictionary:

Dictionary<int, List<string>> dd = new Dictionary<int, List<string>>() {
    {1, new List<string> {"cem"}},
    {2, new List<string> {"cem", "canan"}},
    {3, new List<string> {"canan", "cenk", "cem"}}
};

// This only returns an enumerated array.
var i = (from c in dd
         select c.Value.Count).Select(p=>p);
A: 

How about this?

var l = dd.Select(i => new {i, i.Value.Count});
BigBlondeViking
ohhh didnt see that last comment... oh well
BigBlondeViking
this is good but not responding to my request.
uzay95
+1  A: 
var i = dd.Values.SelectMany(v => v).Count();
Yuriy Faktorovich
Good answer. Interesting approach.
uzay95
A: 

Total count of all list items:

dd.SelectMany(i => i.Value).Count();

List containing individual list counts:

dd.Select(i => i.Value.Count).ToList()
John Rasch
Good answer using with SelectMany.
uzay95
+4  A: 

I believe this will get you the count you want efficiently and clearly. Under the hood it has to iterate through the lists, but to get a total count, there is no way to avoid this.

var i = dd.Values.Sum(x => x.Count);
Timothy Carter