The best way to get a count of IEnumerable

前端 未结 11 1396
悲哀的现实
悲哀的现实 2020-12-14 15:47

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 LIN

相关标签:
11条回答
  • 2020-12-14 16:04

    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;
    
    0 讨论(0)
  • 2020-12-14 16:09

    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.

    0 讨论(0)
  • 2020-12-14 16:09

    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.

    0 讨论(0)
  • 2020-12-14 16:12

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

    1. using System.Linq.Dynamic;
    2. myEnumerable.AsQueryable().Count()
    0 讨论(0)
  • 2020-12-14 16:16

    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.

    0 讨论(0)
  • 2020-12-14 16:21

    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.

    0 讨论(0)
提交回复
热议问题