3

I am trying to get the count of the elements stored in a Ienumerable object. There is no count property for this so how do i get the count for the var object?

Thanks

hWorld
  • 185
  • 2
  • 3
  • 9
  • The `IEnumerable` doesn't necessarily know how many elements it has. The only thing it knows for sure is that it can be enumerated. When (or even if) it ever hits the end is a detail beyond the scope of `IEnumerable`. – Rex M Jun 13 '11 at 18:33
  • 1
    duplicate: http://stackoverflow.com/questions/168901/howto-count-the-items-from-a-ienumerablet-without-iterating – Denis Biondic Jun 13 '11 at 18:30
  • maybe ienumerable isnt the best way to do this... Initially i am trying to use LINQ to XML to read a bunch of elements in an xml. For ex school.Elements("Student"); this returns an ienumerable elements type object. Is there a way to just get it as an arraylist for examle? – hWorld Jun 13 '11 at 18:37
  • use `.ToArray()` or `.ToList()` to get it as a countable object. The reason it returns an `IEnumerable` is because it doesn't execute your selects/filters until you try to do something with it (like `foreach` over it). The `IEnumerable` is just a placeholder; also called a "lazy" implementation. – Rex M Jun 13 '11 at 18:41
  • hmm just tried the .ToList() but it gives an error. Only .ToString() is available? DO i need an addtional cast somewhere? – hWorld Jun 13 '11 at 18:44

4 Answers4

14

You could use the Count() extension method starting from .NET 3.5:

IEnumerable<Foo> foos = ...
int numberOfFoos = foos.Count();

Make sure you've brought it into scope by:

using System.Linq;

And don't worry about performance, the Count() extension method is intelligent enough not to loop through the enumerable if the actual type is for example a static array or a list in which case it would directly use the underlying Length or Count property.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

You cannot do anything with an IEnumerable other than iterate over it.

So, to get the count, you would have to write this code:

int count = 0;
foreach(var item in enumerable) {
    ++count;
}

// Now count has the correct value

To save you the awkwardness, LINQ provides the Count extension method which does just that. You will need to do using System.Linq to use it.

Also, be aware that, depending on the nature of the IEnumerable, it might not be possible to iterate over it more than once (although this is admittedly a rare case). If you have such an object in your hands, counting the number of items will effectively render it useless for future work.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 1
    Actually it's not always required to iterate.http://stackoverflow.com/questions/168901/howto-count-the-items-from-a-ienumerablet-without-iterating/853478#853478 The `Count` extention method actually check to see if it's an `ICollection` and if it is just returns the `Count` property from `ICollection`. – Davy8 Jun 13 '11 at 19:15
0

It is better to call ToList first if you finally want to convert it to a list. Especially when your lambada expression binds an outer variable:

string str = "A,B,C";
int i = 0;
var result= str.Split(',').Select(x=>x+(i++).ToString());


var count = result.Count();
var list =result.ToList();

//Now list content is A3 B4 C5, obviously not you want.
Liang
  • 867
  • 11
  • 13
0

You should use the Count() method. Enumerables have no Count property.

Zebi
  • 8,682
  • 1
  • 36
  • 42