-1

I want to be able to test a count of obj below but not able to do so. when I mouse over obj it shows a count of 3

    <IEnumerable<File> obj = await _mytester.cleanup(myitems)

why cant i do this?

    if(obj.count > 0)

The liine above is giving me an error

Baba
  • 2,059
  • 8
  • 48
  • 81
  • 2
    You could call the `Count()` extension method... `if (obj.Count() > 0) ...`. Tho you will probably want to use `Any()` instead to check if the sequence is not empty. `if (obj.Any()) ...` – Jeff Mercado Mar 06 '20 at 01:11
  • See also https://stackoverflow.com/questions/31815853/should-count-of-an-ienumerable-be-avoided – Peter Duniho Mar 06 '20 at 01:39

1 Answers1

2

You can't do that because count doesn't exist on IEnumerable and that's by design.

You can use IEnumerable.Count() like this:

if (obj.Count() > 0)

But that's horribly inefficient since you're really just checking if there's anything there.

You're better off doing this:

if (obj.Any())

Both of these are provided by LINQ extension methods.

It's very important to note that Count() is iterating through all elements and is nowhere near as efficient as something like ICollection.Count

Zer0
  • 7,191
  • 1
  • 20
  • 34