Friday, 18 October 2013

Expression Lambda example - Find customers in UK

MSDN says "a lambda expression with an expression on the right side is called an expression lambda." Here is an example of Assume you have a List<Customer> where Customer has the following properties.
Customer {
int CustomerId,
string Name, 
string Country, 
string Email,
string Phone
}

If you want to create a List<Customer> where customer country = UK then you can use the below syntax.

List<Customer> ukCustomers =
Customers.FindAll(customer => customer.Country.ToUpper().Equals("UK"))

If you want to do the same thing using LINQ

List<Customer> ukCustomers = from Customer customer in Customers 
                where customer.Country.ToUpper().Equals("UK") 
                select customer 

No comments:

Post a Comment