LINQ way to count elements in each value of dictionary? -
i have dictionary<string, string[]>
, i'm hoping linq way count strings in values. i'm using ol' fashioned for:
int total = 0; (int = 0; < dict.count; i++) { total += dict.elementat(i).value.length; }
i thinking of select()
i'd have ienumerable<string[]>
or 2d array of strings. there better way?
sum1 should work that
int total = dict.sum(d => d.value.length);
here full working demo can test in linqpad or console app if inclined
dictionary<string, string[]> dict = new dictionary<string, string[]>(); for( int = 0; < 10; i++ ) { dict["s"+i] = enumerable.range(0,10).select(s => s.tostring()).toarray(); } int total = dict.sum(d => d.value.length); console.write(total);//100
Comments
Post a Comment