Linq - Question about the Any Method

Suppose I have the following query on the AdventureWorks database:

var result = from customer in Customer where customer.CustomerAddress.Any (ca => ca.Address.City == "Dallas") select new { customer.Individual.Contact.FirstName, Addresses = customer.CustomerAddress }; 

This query will return all customers who live in Dallas. However, I'm not sure why this works. I know that the Any method returns a boolean depending on whether any of the lines in the sequence satisfy the predicate. But used in this way, it seems that it actually returns a sequence of strings that satisfy the predicate. I probably donโ€™t know exactly what is going on here.

However, used as follows, it is easy to understand how "Any" works:

 var result = Customer.Any (c => c.CustomerAddress.Any (ca => ca.Address.City == "Largo" ) ); 

This returns false since clients do not live in Largo.

+6
source share
6 answers

The first request can be read as

Return all customers who have addresses in Dallas.

The response or result of the request is "these are the customers." First request, Any strictly against addresses. Therefore, clients are returned that have addresses that satisfy Any .

Second query as

Do I have customers who have addresses in Largo?

The result is either yes or no (true or false). You applied Any both the addresses and the results of Any addresses. Thus, the first part of the request is โ€œa filter of clients with addresses in Largo,โ€ and the second is โ€œnow everything I want to know if I have such clientsโ€.

Does that make sense now?

+11
source

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.

+4
source

You get a list of strings that satisfy the predicate because you use the Any method as the where clause for the select statement that selects the list of strings.

So your first example says: "Select all customers who have an address where the city address is Dallas." Any does not return a list of strings (it just acts as a condition for the where clause), the select statement.

+1
source

The metric that is defined for each item in Customer (yes or no about each customer). This way you get all customers who have any city address = Dallas.

The second statement says, tell me if any client has a city address = Largo. (Yes or no)

+1
source
  • For each customer at customer
  • When the CustomerAddresses of customer collection has at least one Address with a City field that matches "Dallas"
  • Select "Name and Collection of Client Addresses."
+1
source

Here are both examples rewritten in the method syntax (the first in the mixed syntax in your example, the second already in the method)

  IEnumerable result = Customers .Where(c => c.CustomerAddress.Any(ca => ca.Address.City == "Dallas")) .Select(c=> new { FirstName = c.Individual.Contact.FirstName, Addresses = c.Addresses}; //vs bool result = Customers.Any (c => c.CustomerAddress.Any (ca => ca.Address.City == "Dallas" ) ); 

See assignments? The first statement ends with Select => returns enumerable. The second end with Any => returns bool

+1
source

Source: https://habr.com/ru/post/890671/


All Articles