Generic List Operations


Problem: You need a quick and easy way to manipulate elements contained in a list.

Solution: Lambda Expression, these expressions are used in the same fashion you would use anonymous methods but are less wordy.

Anonymous methods:

employees.FindAll(delegate(employee e1)
    return e1.Name == "Dave"; 

Lambda Expression:

employees.FindAll(e1 => e1.Name == "Dave");
employees.Sort((e1,e2) => e1.LastName.CompareTo(e2.LastName));

For more complex expression use the syntax like (params) => {statment}.

employees.Sort((e1, e2) =>
    if (e1.LastName.CompareTo(e2.LastName) == 0)
        return e1.FirstName.CompareTo(e2.FirstName);
        return e1.LastName.CompareTo(e2.LastName);

For more Info: MSDN