.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Articles >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Lambda Expressions Extension Methods Series 12 - ToLookup()

Posted By:Jean Paul       Posted Date: May 25, 2011    Points: 200    Category: LINQ    URL: http://www.dotnetspark.com  

Lambda Expressions Extension Methods Series 12 - ToLookup()

The extension methods inside Enumerable.cs includes ToLookup() probably the most unused one.  The purpose of the method is to create a lookup list based on key values. 

Operates on: IEnumerable

Returns: ILookup

Please note that the return value is a an interface of ILookup which contains 2 generic arguments.  The first generic argument performs as the key and the values will be an IEnumerable of the original type.


The sample list contains strings of various lengths.

private void ToLookupButton_Click(object sender, EventArgs e)


    IList<string> list = new List<string>();







    // Now there are 6 items in the list with length of 2, 3 and 4


    ILookup<int, string> lookupList = list.ToLookup(s => s.Length);


    // Get the lookup values for length 2

    foreach (string value in lookupList[2])

        MessageBox.Show("Lookup Values for Length 2: " + value);



The output of the application would be:

Lookup Values with length 2 are: as, is


Code Explained

From the above code we can see that a list was created with six string items.  Then the ToLookup() method is called by passing the length as parameter.  The method will return a key value pair where key equals the length and the value an IEnumerable which contains the items corresponding to the key.















Advanced Example

Now let us take a more real time example where we have  a series of Employee objects having Id, Age and Name as the properties.  We need to create a lookup list based on their age.  The result will be having Age as key and the corresponding Employee objects.

private void ToLookupAdvanced_Click(object sender, EventArgs e)


    IList<Employee> list = new List<Employee>();

    list.Add(new Employee() { Id = 1, Age = 20, Name = "Kavin" });

    list.Add(new Employee() { Id = 2, Age = 30, Name = "Alen" });

    list.Add(new Employee() { Id = 3, Age = 20, Name = "Suresh" });

    list.Add(new Employee() { Id = 4, Age = 30, Name = "Jay" });

    list.Add(new Employee() { Id = 5, Age = 20, Name = "Nanda" });


    ILookup<int, Employee> lookupList = list.ToLookup(employee => employee.Age);


    foreach (Employee employee in lookupList[20])




The result of the above code will be Employee objects with Age = 20:

1 Kavin

3 Suresh

5 Nanda

The class structure of Employee would be:

public class Employee


    public int Id;

    public double Salary;

    public int Age;

    public string Name;


    public override string ToString()


        return this.Id.ToString() + " " + this.Name;




Extension Method Snapshot

You can find the snapshot of ToLookup() method usage as provided by Visual Studio.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend