Reputation: 9876
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?
Upvotes: 3
Views: 695
Reputation: 4776
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.
Upvotes: 2
Reputation: 23831
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.
Upvotes: 2
Reputation: 4107
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.
Upvotes: 2
Reputation: 49013
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
.
Upvotes: 4
Reputation: 148664
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();
Upvotes: 2
Reputation: 14591
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.
Upvotes: 2
Reputation: 77354
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.
Upvotes: 2
Reputation: 16983
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.
Upvotes: 1