Any returns bool, but it is sent to the where clause. The where clause takes a logical expression, applies it to each element in the sequence, and returns those elements where the expression is true. Therefore, in the syntax of the extension method, your request translates into:
var result = Customer .Where(customer => customer.CustomerAddress.Any(ca => ca.Address.City == "Dallas")) .Select(customer => new { });
The second example is almost exactly the same, except that instead of where it uses the external Any . Here it is formatted according to the code above:
var result = Customer .Any(c => c.CustomerAddress.Any(ca => ca.Address.City == "Largo"));
Simplification of the further, it becomes even more clear what is happening:
var result1 = Customer .Where(customer => customer.HasAnyAddressHere); var result2 = Customer .Any(customer => customer.HasAnyAddressHere);
We see that the where clause is what really forces your first request. You can write it in English as:
Give me all the customers who have at least one address in Dallas. Ignore their other addresses. If the customer does not have addresses in Dallas, filter his / her results.
As you can see, this requests a list of customers. The second way:
Is there at least one customer who has at least one address in Dallas?
This is a yes / no question, so it returns true or false.
source share