For example, if I had a Linq to SQL data context, or if I had ADO.NET Entity Framework entities that mapped to a database table, and I want to test for a single Customer…
Is there much difference between:
MyDatabaseContext.Customers.Any(c => c.CustomerId == 3)
and
MyDatabaseContext.Customers.Where(c => c.CustomerId == 3)
.Any<> – return type bool
.Where<> – return type IQueryable
EDIT: Corrected question wording after accepting answer from Fredrik Mörk – thanks.
Check the documentation again:
Any<>returns aboolindicating whether at least one item meets the criteriaWhere<>returns anIEnumerablecontaining the items that meet the criteriaThere may be a performance difference in that
Anystops as soon as it can determine the result (when it finds a matching item), whileWherewill need to always loop over all items before returning the result. So if you only need to check whether there are any matching items,Anywill be the method for the job.