score:3

Accepted answer

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.

score: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.

score: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.

score: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.

score: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();

score: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.

score: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.

score: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.


Related Articles