Reputation: 17
I created a dictionary with the key of type string and the values of type string[]. i am having a hard time figuring out how to get the count of the array in the dict. so i know dict.count returns the number of dictionary pairs but not sure how to get the count of the array
Dictionary<string, string[]> csvFileNameSheetName = new Dictionary<string, string[]>();
i tried this but obviously wont work
for (int idx = 0; idx < csvFileNameSheetName.Values.Count; idx++)
what i need is something like csvFilenameSheetName.values.Values.count
Upvotes: 1
Views: 639
Reputation: 895
int count = csvFileNameSheetName.Aggregate(0, (r, kvp) => r = r + kvp.Value.Count());
OR
var count = 0;
foreach(var strArr in csvFileNameSheetName.Values)
{
count += strArr.Length;
}
Upvotes: 0
Reputation: 70523
Use linq
int c = csvFileNameSheetName.Aggregate(0, (r, i) => r = r + i.Value.Count());
Upvotes: 0
Reputation: 3291
Use Linq's SelectMany to put all the string[]
items into a single enumeration:
csvFileNameSheetName.SelectMany(kvp=>kvp.Value).Count(); //using System.Linq
Or for better performance you can use Sum to add up the counts:
csvFileNameSheetName.Sum(kvp=>kvp.Value.Length); //using System.Linq
Upvotes: 1