LINQ Extension Methods - Any () vs. Where () vs. Exists ()

Unfortunately, the names of these methods make terrible search terms, and I could not find a good resource that explains the difference between these methods - as in when to use each.

Thank.

Edit:

The type of request I'm trying to fully understand looks something like this:

context.Authors.Where(a => a.Books.Any(b => b.BookID == bookID)).ToList(); 

And thanks to everyone who answered.

+48
c # linq
Sep 13 '10 at 18:24
source share
6 answers

Where returns a new sequence of elements matching the predicate.

Any returns a boolean value; there is a version with a predicate (in this case, it returns whether any elements match) and a version without (in this case, it returns whether the request still contains any elements).

I'm not sure about Exists - this is not a standard LINQ query statement. If there is a version for the Entity Framework, perhaps it checks for key-based existence - a kind of specialized form, Any ? (There Exists method in List<T> , which is similar to Any(predicate) but precedes LINQ.)

+78
Sep 13 '10 at 18:28
source share

context.Authors.Where (a => a.Books.Any (b => b.BookID == bookID)). ToList ();

a.Books is a list of books by this author. The property is automatically created by Linq-to-Sql if you have a foreign key relationship.

So, a.Books.Any(b => b.BookID == bookID) translates as "Does any of the books of this author have the identifier bookID", which makes the full expression "Who are the authors of the book with id bookID?"

It can also be written as

  from a in context.Authors join b in context.Books on a.AuthorId equal b.AuthorID where b.BookID == bookID select a; 

UPDATE: Any() as far as I know, returns bool . Its effective implementation:

  public Any(this IEnumerable<T> coll, Func<T, bool> predicate) { foreach(T t in coll) { if (predicte(t)) return true; } return false; } 
+9
Sep 13 '10 at 19:19
source share

Just to find it next time, here's how you look for Linq enumerated extensions. The methods are the static methods of Enumerable, so Enumerable.Any, Enumerable.Where and Enumerable.Exists.

Since the third does not return any useful result, I found that you had in mind List.Exists, thus:

I also recommend hookedonlinq.com as it has very detailed and understandable guides, as well as clear explanations of the behavior of Linq methods in relation to postponement and laziness.

+7
Sep 13 '10 at 18:28
source share

Any logical function that returns true if any of the objects in the list satisfies the condition specified in the function parameters. For example:

 List<string> strings = LoadList(); boolean hasNonEmptyObject = strings.Any(s=>string.IsNullOrEmpty(s)); 

Where is a function that returns a list with all the objects in the list that satisfy the condition specified in the function parameters. For example:

 IEnumerable<string> nonEmptyStrings = strings.Where(s=> !string.IsNullOrEmpty(s)); 

There is - basically the same as any, but not general - it is defined in the List class, and Any is defined in the IEnumerable interface.

+3
Sep 13 '10 at 18:32
source share

IEnumerable introduces quite a few extensions that help you pass in your own delegate and call the resulting result from IEnumerable. Most of them are Func type

Func takes an argument T and returns TResult.

When

Where is Func: so it takes IEnumerable from T and returns bool. Where ultimately will return IEnumerable from T for which Func returns true.

So, if you have 1,5,3,6,7 as IEnumerable and you write .where (r => r <5), it will return the new IEnumerable from 1,3.

Any - Func is basically similar to a signature, but returns true only when any of the criteria returns true for IEnumerable. In our case, it will return true, since there are several elements with r <5.

Exists - A predicate, on the other hand, will return true only when any of the predicates returns true. Therefore, in our case, if you pass .Exists (r => 5) will return true, since 5 is an element present in IEnumerable.

+1
Sep 13 2018-10-18
source share

Any () returns true if any of the elements in the collection matches your predicate criteria.

Where () returns the enumerated of all elements in the collection that match your predicate criteria.

Exists () does the same as everything except the older implementation that was there on IList before Linq.

0
Sep 13 '10 at 18:29
source share



All Articles