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 

1. msdn: enumerable.sum method


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -