Sean Chambers
Sean Chambers

Reputation: 8680

The best way to get a count of IEnumerable<T>

Whats the best/easiest way to obtain a count of items within an IEnumerable collection without enumerating over all of the items in the collection?

Possible with LINQ or Lambda?

Upvotes: 37

Views: 63831

Answers (12)

James Groom
James Groom

Reputation: 365

From .NET 6, you can use TryGetNonEnumeratedCount. For older targets, you can get similar results with a simple type check:

public static bool TryGetNonEnumeratedCountPolyfill<T>(this IEnumerable<T> source, out int count) {
    if (source is ICollection<T> genericCollection) {
        count = genericCollection.Count;
        return true;
    }
    if (source is ICollection collection) {
        count = collection.Count;
        return true;
    }
    count = default;
    return false;
}

Upvotes: 0

Mike Meinz
Mike Meinz

Reputation: 506

When I want to use the Count property, I use ILIST which implements IEnumerable and ICollection interfaces. The ILIST data structure is an Array. I stepped through using the VS Debugger and found that the .Count property below returns the Array.Length property.

IList<string> FileServerVideos = Directory.GetFiles(VIDEOSERVERPATH, "*.mp4"); 
if (FileServerVideos.Count == 0)
    return;

Upvotes: 0

Brian Leahy
Brian Leahy

Reputation: 35507

You will have to enumerate to get a count. Other constructs like the List keep a running count.

Upvotes: 17

Timmy Fuller
Timmy Fuller

Reputation: 75

An IEnumerable will have to iterate through every item. to get the full count. If you just need to check if there is one or more items in an IEnumerable a more efficient method is to check if there are any. Any() only check to see there is a value and does not loop through everything.

IEnumerable myStrings = new List(){"one","two", "three"};

bool hasValues = myStrings.Any();

Upvotes: 1

Mukesh
Mukesh

Reputation: 67

Use this.

IEnumerable list =..........;

list.OfType<T>().Count()

it will return the count.

Upvotes: 5

Soundararajan
Soundararajan

Reputation: 21

It also depends on what you want to achieve by counting.. If you are interested to find if the enumerable collection has any elements, you could use

myEnumerable.Any() over myEnumerable.Count() where the former will yield the first element and the later will yield all the elements.

Upvotes: 2

Moustafa Deif
Moustafa Deif

Reputation: 1

The best solution -as I think is to do the following:

  1. using System.Linq.Dynamic;
  2. myEnumerable.AsQueryable().Count()

Upvotes: 0

Keith
Keith

Reputation: 155622

If you need to count and then loop you may be better off with a list.

If you're using count to check for members you can use Any() to avoid enumerating the entire collection.

Upvotes: 0

ICR
ICR

Reputation: 14162

The solution depends on why you don't want to enumerate through the collection.

If it's because enumerating the collection might be slow, then there is no solution that will be faster. You might want to consider using an ICollection instead if possible. Unless the enumeration is remarkably slow (e.g. it reads items from disk) speed shouldn't be a problem though.

If it's because enumerating the collection will require more code then it's already been written for you in the form of the .Count() extension method. Just use MyEnumerable.Count().

If it's because you want to be able to enumerate the collection after you've counted then the .Count() extension method allows for this. You can even call .Count() on a collection you're in the middle of enumerating and it will carry on from where it was before the count. For example:

foreach (int item in Series.Generate(5))
{
    Console.WriteLine(item + "(" + myEnumerable.Count() + ")");
}

will give the results

0 (5)

1 (5)

2 (5)

3 (5)

4 (5)

If it's because the enumeration has side effects (e.g. writes to disk/console) or is dependant on variables that may change between counting and enumerating (e.g. reads from disk) [N.B. If possible, I would suggest rethinking the architecture as this can cause a lot of problems] then one possibility to consider is reading the enumeration into an intermittent storage. For example:

List<int> seriesAsList = Series.Generate(5).ToList();

All of the above assume you can't change the type (i.e. it is returned from a library that you do not own). If possible you might want to consider changing to use an ICollection or IList (ICollection being more widely scoped than IList) which has a Count property on it.

Upvotes: 18

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

There's also IList or ICollection, if you want to use a construct that is still somewhat flexible, but also has the feature you require. They both imply IEnumerable.

Upvotes: 2

Greg Hurlman
Greg Hurlman

Reputation: 17804

Not possible with LINQ, as calling .Count(...) does enumerate the collection. If you're running into the problem where you can't iterate through a collection twice, try this:

List<MyTableItem> myList = dataContext.MyTable.ToList();
int myTableCount = myList.Count;

foreach (MyTableItem in myList)
{
   ...
}

Upvotes: 0

Konrad Rudolph
Konrad Rudolph

Reputation: 545518

In any case, you have to loop through it. Linq offers the Count method:

var result = myenum.Count();

Upvotes: 23

Related Questions