I want to get the total number of items in the List
s 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);
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);
var i = dd.Values.SelectMany(v => v).Count();
Total count of all list items:
dd.SelectMany(i => i.Value).Count();
List
containing individual list counts:
dd.Select(i => i.Value.Count).ToList()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With