Wednesday, March 26, 2008

Using Lambda Expressions and Linq for manipulation of collections

In the previous article using anonymous method predicates to filter data in Generics, we saw how anonymous methods will ease the job of a developer instead of using delegates.
With the introduction of C# 3.0, we have much easier and better approaches for managing collections like LINQ and Lambda expressions. In this article I will try to use the new language features introduced in C# 3.0 to manage collections.
Lambda Expressions for method implementations provide a much easier and effective way for managing collections in .NET.
For example we can take the instance of sorting a Generic List of Customer objects.

public class Customer
{
public String FirstName { get; set; }
public String LastName { get; set; }
public Int32 CustomerID { get; private set; }

public Customer(Int32 id)
{
CustomerID = id;
}
public Customer(){ }

public override string ToString()
{

return FirstName + ", " + LastName + ", " + CustomerID.ToString();
}
}


For sorting this collection based on the FirstName using anonymous methods I use to code like

List _customers = new List
{
new Customer(1) { FirstName= "Prajeesh", LastName="Prathap" },
new Customer(2) { FirstName = "Sreejith", LastName="P.R" },
new Customer(3) { FirstName = "John", LastName = "Mitchel" },
};

_customers.Sort(delegate(Customer c, Customer c1)
{ return c.FirstName.CompareTo(c1.FirstName); });


By the introduction of Lambda expressions, I can change my code to use Lambda expressions instead of anonymous methods. When I use Lambda expressions the C# compiler will translate the code to an instance of a delegate as in the case of anonymous methods. The benefit of using Lambda expressions over anonymous methods is that anonymous methods require parameter type declarations to be explicitly stated, whereas Lambda expressions does not require parameter types and instead allow them to infer based on usage.

For example: consider the following code that uses Lambda expressions for sorting the _customers collection created above.

_customers.Sort((c, c1) => c.FirstName.CompareTo(c1.FirstName));


You can also explicitly declare Lambda parameters like

_customers.Sort((Customer c, Customer c1) => c.FirstName.CompareTo(c1.FirstName));

Using Enumerable class methods to manage collections

The Enumerable class provides a set of static methods for querying objects that implement IEnumerable.

We can use the Enumerable.OrderBy method for sorting a collection of objects as

private IEnumerable SortCustomers(List _customers)
{
return Enumerable.OrderBy(_customers, (cust) => cust.FirstName);
}

Or

private IEnumerable SortCustomers(List _customers)
{
return _customers.OrderBy((cust) => cust.FirstName);
}


Where OrderBy is an extension method declared in the Enumerable Class in System.Linq namespace.

Similarly for Filtering the Customer object based on FirstName we can use The Enumerable.Where method to obtain customers whose FirstName starts with “John”

IEnumerable _filteredCustomers =
Enumerable.Where(_customers, (cust) => cust.FirstName.StartsWith("John"));

Or

IEnumerable _filteredCustomers = _customers.Where((cust) => cust.FirstName.StartsWith("John"));

In the next series of this article we will see the usage of LINQ and how we can implement LINQ in our example to mange collections.

No comments: