Lambda Expressions Are Wonderful

Lambda expressions are part of .Net 3.0. They are used extensively in Linq, but they can also be used on their own. With Lambda expressions, filtering and sorting Lists has become a lot easier.
 
In my examples, I'm going to filter and sort Lists of Employee objects,
  1. public class Employee  
  2. {  
  3.     public string FirstName {set;get;}  
  4.     public string LastName {set;get;}  
  5.     public decimal Salary {set;get;}  
  6.     public bool IsManager {set;get;}  
  7. }  

FindAll( ) 

 
FindAll( ) Suppose I have a List of Employees, and I want to find all the managers. At one point I would have written code like this,
  1. List<Employee> managers = new List<Employee>( );  
  2. foreach (Employee employee in employees)  
  3. {  
  4.     if (employee.IsManager == true)  
  5.         managers.Add(employee);  
  6. }  
The new syntax with Lambda expressions is clean and simple:
  1. List<Employee> managers = employees.FindAll(employee => employee.IsManager == true);  
Note that the term "employee" was used to make things clear. I could have used any other name. For example:
  1. List<Employee> managers = employees.FindAll(e => e.IsManager == true);  

Where( )

 
Where( ) works exactly the same way as FindAll( ). In some cases, using Where( ) instead of FindAll( ) can make your code easier to understand.
 
Here, for example, is a different way to find all the managers,
  1. using System.Linq;  
  2. foreach (Employee employee in employees.Where(e => e.IsManager == true))  
  3. {  
  4.     // do something  
  5. }  

Find( )

 
Find( ) returns the first object in a list which meets my search criteria. If no object meets my criteria, it returns null.
  1. Employee firstManager = employees.Find(employee => employee.IsManager == true);  
  2. if (firstManager == null)  
  3.     Console.WriteLine("No managers found in list.");  

ForEach( )

 
ForEach( ) can be used to perform an operation on each item in a List. In this example we are adding 100 to the Salary of each Employee:
  1. employees.ForEach(e => e.Salary += 100);  

OrderBy( ) and OrderByDescending( )

 
OrderBy( ) returns a List sorted in ascending order. OrderByDescending( ) returns a List sorted in descending order.
 
This only works if there is an appropriate method to do the comparison. For basic data types (e.g., string, int, decimal, etc.), Lambda expressions make sorting easy: 
  1. using System.Linq; List<Employee> sortedList = employees.OrderBy(e => e.Age).ToList();  
Note that the original List, employees, is unchanged. Of course, you could sort the employees List itself as follows:
  1. employees = employees.OrderBy(e => e.Age).ToList();  
To sort in descending order:
  1. List<Employee> sortedList = employees.OrderByDescending(e => e.Age).ToList();  

Sort( )

 
Sort( ) provides an alternative to OrderBy( ):
  1. employees.Sort((e1, e2) => e1.DOB.CompareTo(e2.DOB));  
Note that Sort( ) operates on the orginal List, employees. That is, there is no need to do this:
  1. employees = employees.Sort((e1, e2) => e1.DOB.CompareTo(e2.DOB));  
because employees is already sorted.
 
Getting Fancy If I make an enum like this,
  1. public enum SortOrder { Ascending, Decending }  
and I add this method to the Employee class,
  1. public static void Sort<TKey>(ref List<Employee> list, Func<Employee, TKey> sortKey,   
  2.     SortOrder sortOrder)  
  3. {  
  4.     if (sortOrder == SortOrder.Ascending)  
  5.         list = list.OrderBy(sortKey).ToList();  
  6.     else  
  7.         list = list.OrderByDescending(sortKey).ToList();  
  8. }  
Now I can sort my List of Employees like this,
  1. Employee.Sort(employees, e => e.Salary, SortOrder.Ascending);  
or this:
  1. Employee.Sort(employees, e => e.LastName, SortOrder.Descending);  
In this article, I've only discussed the use of Lambda expressions to sort and filter Lists of objects.
 
If that were the only use for Lambda expressions, they would still be a major time saver - a fantastic new addition to .Net. Lambda expressions, however, have many uses. 
 
Once you get started, you will wonder how you ever managed without them. Enjoy.