tags:

views:

143

answers:

5

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 select a single Customer...

What is the difference between:

MyDatabaseContext.Customers.Any(c => c.CustomerId == 3)

and

MyDatabaseContext.Customers.Where(c => c.CustomerId == 3)

Are both even valid?

.Any<> - return type bool

.Where<> - return type IQueryable

EDIT: Corrected question wording after accepting answer from Fredrik Mörk - thanks.

+14  A: 

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.

Fredrik Mörk
nitpick: Where will return an IEnumerable when used in a Linq to objects context. The OP gave the example as a Linq to SQL context so it would return an IQueryable. Not that it matters much. The best would be to write `IEnumerable/IQueryable`, or sequence.
Martinho Fernandes
@Martinho: good points. However, I would still argue that `Where<>` returns an `IEnumerable` for a couple of reasons: one is that `IQueryable` inherits `IEnumerable`. Another reason is that when dealing with interfaces I usually prefer to stick to the interface defined by the contract, rather than what is *actually* returned by the underlying provider. That way the code is less dependent on the internals of the called methods, making it more portable.
Fredrik Mörk
"while Where will need to always loop over all items before returning the result" Not always true. For instance, in LINQ to objects, since the returned `IEnumerable<T>` is evaluated lazily, if you add a `.Any()` to `.Where(something)`, it **won't** loop over all items.
Mehrdad Afshari
+2  A: 

Any() returns a bool. I.e. are there any elements matching the condition. Use Any() if you just want to know if you have elements to work with. E.g. prefer Any() over Count() == 0 for instance as the latter will possible enumerate the entire sequence to find out if it is empty or not.

Where() returns a sequence of the elements matching the condition.

Brian Rasmussen
+3  A: 

Any returns a bool while Where returns an IQueryable. Being lazy, one would expect Any to terminate as soon as one satisfying element is found (returning true) while Where will search them all.

If you want to select a single customer, Single is what you are looking for.

Martinho Fernandes
+2  A: 

Any<> checks whether any items satisfy the criterion, i.e. returns bool, meaning that it only has to find the first item, which can be very fast. Whereas Where<> enumerates all the items that satisfy the condition, meaning that it has to iterate the whole collection.

Saulius
+1  A: 

Any tests the lambda/predicate and returns true/false

Where returns the set of objects for which lambda/predicate holds true as IQueryable

Perpetualcoder