In LINQ, what is the main difference/usefulness between .Any<> and .Where<> to test for records existance
.net, linq
Solution
Check the documentation again:
- `Any<>` returns a `bool` indicating whether at least one item meets the criteria
- `Where<>` returns an `IEnumerable` containing the items that meet the criteria
There may be a performance difference in that `Any` stops as soon as it can determine the result (when it finds a matching item), while `Where` will need to always loop over all items before returning the result. So if you only need to check whether there are any matching items, `Any` will be the method for the job.
Problem
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.