Home > Enterprise >  Need to get count of array items in my dictionary
Need to get count of array items in my dictionary

Time:06-02

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

CodePudding user response:

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

CodePudding user response:

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;      
}

CodePudding user response:

did you try this: csvFileNameSheetName.Count?

CodePudding user response:

Use linq

int c = csvFileNameSheetName.Aggregate(0, (r, i) => r = r    i.Value.Count());
  • Related