3

I use ADO.NET entity framework and very often there are code snippets like this :

List<Sole> entity = soleService.All()
    .Where(s => (s.ShoeLastID == shoeLastID) && (s.Status == 20))
    .ToList();

Since now I haven't think much about it and just made this check:

if (entity.Count > 0)

believing that it is enough. Now I see that many people check with Any() and for null. How to be sure at a certain situation what kind of checks I need and in this certain scenario which as I said - I use very often is if (entity.Count > 0) enough?

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
Leron
  • 9,546
  • 35
  • 156
  • 257

8 Answers8

3

if (entity.Count > 0) or if (entity.Any()) are identical in you case. As you already fetched all the data from the DB, the list has been built and you knows its size. So .Count property doesn't iterate over anything.

In the other hand, do not call the .Count() IEnumerable extension if you didn't fetched all data, because it'll enumerate items for nothing.

Use instead:

bool test = soleService.All()
    .Any(s => (s.ShoeLastID == shoeLastID) && (s.Status == 20));

if (test)
{
...
}

Also, LINQ extensions won't return null but an empty IEnumerable, so don't check for null.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • `if (entity.Count > 0) or if (entity.Any()) are identical in you case`. No it's not! .Count() will work slower than Any()! – Maris Apr 09 '13 at 08:53
  • 3
    @Maris .Count() extension method is very different from the .Count property of IList – ken2k Apr 09 '13 at 08:55
  • `Count()` does not enumerate items, if the object is an `ICollection`. So the performance impact between `Count` and `Count()` is only the time used for boxing. – Andy Feb 16 '15 at 00:09
2

If you have a .ToList() call, then the list is always a list. Maybe empty, but never null.

The check for .Any() instead of .Count() > 0 is a performance improvement for most containers or enumerables because .Any() will only touch the first element if there is one. .Count() would need to count through your container to the end although you are not interested in the result, only in the fact that it's not zero.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
2

Depends on what you need.

If you just want to know if any of entities match your predicate then use Any(), as it will return immediately upon finding the first matching entity. Count() / Count will need to process all the entities which will typically be much slower.

Also prefer Linq's Count() to List Count as it doesn't have to create the full list in the memory, which can be very expensive with large result sets.

Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
2

Any() will provide better solution cause it stopse after first matching.

in addition

I would suggest also to do ToList() only if Any() is true.

Youll save (micro) performance.

var t = soleService.All()  .Where(s => (s.ShoeLastID == shoeLastID) && (s.Status == 20));
if  (t.Any()) entity =t.ToList();
Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
2

entity.Any() - will return true if there is any entities in your collection. entities.Count() == 0 will do the same. But I would recommend to use any because it will work faster. Because Count will return the amount of data in the collection, but any will trigger on the first item found in your collection. But if you are not sure that your collection is initialized I would recommend you to use next construction:

if(entity!=null && entity.Any())
{
   //Do something. You will get her always without error, And you will be 100% sure that your collection is not empty and it is initialized
}

Hope it helps.

Maris
  • 4,608
  • 6
  • 39
  • 68
2

When you call if (entity.Count > 0) but entity == null, you will get an exception because .Count does not exist while entity is not initialized.

Andy
  • 3,997
  • 2
  • 19
  • 39
2

Of course a list can be null or empty. If you attempt to create a List<Sole> using LINQ as above and the soleService could be null, in which case you will get a NullReferenceException. So I would check that the soleService is not null or empty first. So

List<Sole> entity = null;
// ...
if (soleService != null && soleService.Count > 0)
    entity = soleService.All()
        .Where(s => (s.ShoeLastID == shoeLastID) && (s.Status == 20))
        .ToList();

I hope this helps.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
1

entity.Any() is better choice. And you don't need to invoke .ToList() since this wll take all the data from the database and then just check its count.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123