Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count lists inside another list List<List<KeyValuePair<string, double>>>

Now this is example list :

List<List<KeyValuePair<string, double>>> dblWordFreqByCluster = 
    new List<List<KeyValuePair<string, double>>>();

What i want is getting the list count at this main list (dblWordFreqByCluster). Which means getting count of List<KeyValuePair<string, double>> lists.

I can count them via making foreach iteration which i don't want to because i suppose that would cause unnecessary performance loss.

like image 223
MonsterMMORPG Avatar asked Dec 04 '22 04:12

MonsterMMORPG


2 Answers

Using LINQ you could do

int totalCount = dblWordFreqByCluster.Sum(c => c.Count);

However, that isn't much different than using a foreach loop, but it would be less verbose and just as easy to read.

like image 189
Adam Gritt Avatar answered Dec 05 '22 17:12

Adam Gritt


A simple:

 List<List<KeyValuePair<string, double>>> dblWordFreqByCluster = new List<List<KeyValuePair<string, double>>>();
 int count = dblWordFreqByCluster.count;

Should work...

Edited: I thought it was java ;)

like image 23
Philippe Avatar answered Dec 05 '22 17:12

Philippe