Count is required when enumerable is not a collection
See original GitHub issueExtension method WithProgress will throw exception is collection has no items
else if (count < 0) Count is required when enumerable is not a collection .
You receive and IEnumerable and return an IEnumerable implementation named progress enumerable.
Instead of throwing, could you just return the enumerable parameter ?
Suggested implementation :
public static IEnumerable WithProgress(this IEnumerable enumerable, IProgressBar progressBar, int count = -1)
{
if (enumerable is ICollection)
{
count = ((ICollection)enumerable).Count;
}
else if (count < 0)
{
return enumerable;
}
return new ProgressEnumerable(enumerable, progressBar, count);
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
IEnumerable doesn't have a Count method
IEnumeration does not have a method called Count() . It's just a kind of "sequence of elements". Use for example List if you...
Read more >CA1829: Use Length/Count property instead of ...
Length or Count property does not enumerate the collection, hence is more efficient. This rule flags Count calls on the following collection ...
Read more >Enumerable.Count Method (System.Linq)
Returns a number that represents how many elements in the specified sequence satisfy a condition.
Read more >C# ienumerable.count() raises an invalidcastexception
That is correct, IEnumerable does not support Count property. IEnumerable only supports iteration of collections. You do need to cast to a ...
Read more >C#: IEnumerable, yield return, and lazy evaluation
First() to try to find a specific item in the collection? You may not need to run all the code in the iterator...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s probably better to use more specific
WithProgress
extension methods, e.g.:So when using
ICollection
, it automatically uses the count, otherwise you just have to pass it manually (e.g. in case of anIReadOnlyCollection
, adding this as additional method would make the signature ambiguous). And ifcount
is less than 0, it should throw anArgumentOutOfRangeException
…I was considering to write
but I’m not sure that is always a better option than materialization (for example, for complex DB queries), and even if any enumerator is guaranteed to be allowed enumerating twice, so it is better to specify that explicitly.