Do you absolutely need these huge collections in your business layer?
Where do these huge collections come from? If for instance, they come from a database, then it might be a good idea to filter/count/sum the result on the database-side of things. This will save loads of processing-power and memory.
Is your program absolutely required to handle these enormous collections?
If not, then try to optimize the sizes of your collections before they enter your business-layer.
A somewhat C#-specific answer:
If performance is of enormous concern then you will have to implement some of these methods, if it's just a "one-time"-thing, just use
foreach with a local
count-variable. However; just calling
.Count() on a large collection is not an enormously heavy operation in itself, as long as the enumeration of each item doesn't take enormous amount of processing-power/time, in which case, you should probably be careful of how many items you enumerate (i.e. use
Take). So creating methods like these might in the end be considered micro-optimization, that is if the collection will always contain 1 million items or less.
Another way to tackle the problem is to enumerate the
IEnumerable<T> to a collection of some sort –
List<T>, for example.
This requires just one enumeration, which will yield you information about the collection, such as
.Count and indexed access to items within it.
This of course depends on whether or not you are going to perform multiple operations on the collection, since it must be enumerated at least once.
Try to be pragmatic, ask yourself the simple questions. Will I need indexed access to the items? Do I need the count? Do I need to make sure there are at least fifteen items? Will i need to perform multiple enumerations of the collection? Will this collection be enormous? Then simply work from there, there is no magical correct way that always works, you absolutely must do trade-offs.
At one point in the code, it might be a good idea to iterate just 10 items, just to check if it contains 10 items or more, at another place; calling
Count() might be a better idea.
At one point in the code, enumerating your collection to a
List<T> might be a great idea, at other points – it might not.
Also, C# already has some great built in "i only want X"-items within Linq, for instance
if (hugeCollection.Take(4).Count() == 4)
This will not enumerate the entire collection, just the first 4 items, twice. Which for small collections might not be that great, but for larger collections, sure.
A C# tidbit:
.Count() is faster depends on whether the call is being made on an
ICollection<T> or not. For
ICollection<T>, Microsoft went ahead and optimized it, looking at the
.Count-property; which is marginally faster than calling
.Any(), regardless of the collections size – since the collection will never have to be enumerated.