What does this LINQ query do?

Here are two classes of C # ...

public class Address  
{  
    public string Country;  
    public string City;  
}

public class Traveller
{    
    public string Name;
    public List<Address> TravelRoute;
}

... and a list of data (filled out somewhere) ...

List<Traveller> Travellers;

... and then this LINQ query:

var result = from t in Travellers
             where t.TravelRoute.Any(a => a.Country == "F")
             select t;

foreach (var t in result)
    System.Console.WriteLine(t.Name);

I do not understand the request: what does the function "Any" mean and what does the operator "=>" do?

Can someone explain to me what is going on in this code? Thank!

+5
source share
5 answers

Line

a => a.Country == "F"

will translate to something like this if it's a separate LINQ statement:

From a as Adress in t.TravelRoute
Where a.Country = "F"
Select a

Any means: Any. In other words, it will return true if ANY of the objects in travelRoute has the Country property for "F"

Hope that helps

+1
source

, TravelRoute , "F".

Any true, - , . , Func<>, bool. , , , .

Where, .

The = > . .

+7
0

Anyone is a filter method that says: "Accept any items that meet the following criteria." Business a => ... means "Given the parameter a, here is the method", as others have said, it is used for lambda expressions. Edit: changed "property" to "parameter"

0
source

All Articles