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.


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.


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.


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.


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


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.


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.


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

i hope this helps.

Related Query