Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Lambda Expressions Extension Methods Series 12 – ToLookup()

Posted by Paul on May 26, 2011


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<T1, T2>

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.

Example

The sample list contains strings of various lengths.

private void ToLookupButton_Click(object sender, EventArgs e)

{

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

list.Add(“as”);

list.Add(“is”);

list.Add(“has”);

list.Add(“can”);

list.Add(“this”);

list.Add(“that”);

// 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.

Key

Values

2

as

 

is

3

has

can

4

this

 

that

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])

MessageBox.Show(employee.ToString());

}

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.

clip_image002

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s