Useful lamda exp

1)...
Customer foundCustomer = null;
foundCustomer = custList.FirstOrDefault(c =>
c.CustomerId == 4);
Debug.WriteLine(foundCustomer);

The lambda expression syntax in C# looks like this:

c => c.CustomerId == 4

The code begins with the set of parameters to the lambda expression. The => is the “goes to” or lambda operator. The remainder of the code is the expression itself. In this case, checking for the item in the list where CustomerId is 4.


2)...

Customer foundCustomer = null;
var query = from c in custList
where c.CustomerId == 4
select c;
foundCustomer = query.FirstOrDefault();
Debug.WriteLine(foundCustomer);


3)...

Customer foundCustomer = null;
foreach (var c in custList)
{
if (c.CustomerId == 4)
{
foundCustomer = c;
break;
}
}
Debug.WriteLine(foundCustomer);

Read Users' Comments (0)

0 Response to "Useful lamda exp"

Post a Comment