4

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.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • using foreach will likely not affect performance. Linq is likely to be slower but more compact. Maybe you can consider wrapping KeyValuePair inside a class with a count property as well. It might help cleanup things. – NoviceProgrammer Apr 05 '12 at 12:47

2 Answers2

15

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.

Adam Gritt
  • 2,654
  • 18
  • 20
2

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

Philippe
  • 103
  • 1
  • 7