question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Count is required when enumerable is not a collection

See original GitHub issue

Extension 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:open
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ronaldbarendsecommented, May 15, 2019

It’s probably better to use more specific WithProgress extension methods, e.g.:

public static IEnumerable<T> WithProgress<T>(this ICollection<T> collection, IProgressBar progressBar)
{
    return collection.WithProgress<T>(progressBar, collection.Count);
}

public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, IProgressBar progressBar, int count)
{ }

So when using ICollection, it automatically uses the count, otherwise you just have to pass it manually (e.g. in case of an IReadOnlyCollection, adding this as additional method would make the signature ambiguous). And if count is less than 0, it should throw an ArgumentOutOfRangeException

0reactions
pieceofsummercommented, Dec 19, 2016

I was considering to write

    }
    else if (count < 0)
    {
        count = enumerable.Count();
    }

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found