Practical Usage of Using ToLookup() Method in LINQ C#

Step 1: Used NameSpaces:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;

Step 2: Explanation about ToLookup()

  1. It creates a Key based on the user's choice at runtime. In this article I have used the length of the string as the Key. So it stores data and creates the key based on the length of the string.
    E.g. "Lajapathy" has a length of 9, so the key is created with the value 9 and the value is "Lajapathy".
  2. Exactly the same concept of Dictionary<K, T>, but the key is not static; it is dynamic.
  3. Setting key at Runtime.
  4. It is very useful if using complex data type.
  5. It is useful to get data fast, because it stores as Index key.
  6. It is a KeyValue<K, T> pair.

Step 3: Used String List for Demo:

    public static List<string> GetStringList()
        List<string> list = new List<string>();
        return list;

Step 4: Code Snippet using Simple Data Type:

static void SimpleLookup()
        List<string> list = GetStringList();
        //Sets KeyValue pair based on the string length.
        ILookup<int, string> lookup = list.ToLookup(i => i.Length);
        HttpContext.Current.Response.Write("String length=7" + "<br/>");
        //Iterates only string length having 7.
        foreach (string temp in lookup[7])
            HttpContext.Current.Response.Write(temp + "<br/>");
        HttpContext.Current.Response.Write("<br/>String length=9" + "<br/>");

        //Iterates only string length having 9.
        foreach (string temp in lookup[9])
            HttpContext.Current.Response.Write(temp + "<br/>");

Step 5: Output of Simple Data Type


Step 6: Used Employee List for Demo

    /// <summary>
Employee list
    /// </summary>
    public static List<Employee> EmployeeList()
        List<Employee> emp = new List<Employee>();
        emp.Add(new Employee { ID = 100, Name = "Lajapathy", CompanyName = "FE" });
        emp.Add(new Employee { ID = 200, Name = "Parthiban", CompanyName = "FE" });
        emp.Add(new Employee { ID = 400, Name = "Sathiya", CompanyName = "FE" });
        emp.Add(new Employee { ID = 300, Name = "Anand Babu", CompanyName = "FE" });
        emp.Add(new Employee { ID = 300, Name = "Naveen", CompanyName = "HCL" });
        return emp;

Step 7: Code Snippet using Complex Data Type (Object)

    public static void AdvaceLookupLookup()
        //Employee collection
        List<Employee> empList = EmployeeList();
        HttpContext.Current.Response.Write("Here Key based on ID<br/>"); 

        //Creating KeyValue pair based on the ID. we can get items based on the ID.
        ILookup<int, Employee> lookList = empList.ToLookup(id => id.ID); 

        //Displaying who having the ID=100.
        foreach (Employee temp in lookList[100])
            HttpContext.Current.Response.Write(temp.Name + "<br/>");
        HttpContext.Current.Response.Write("Here Key based on CompanyName<br/>"); 

        //Creating KeyValue pair based on the CompanyName.
        //we can get items based on the CompanyName.
        ILookup<string, Employee> lookList2 = empList.ToLookup(id => id.CompanyName);
        //Displaying who are employed in CompanyName=FE.
        foreach (Employee temp in lookList2["FE"])
            HttpContext.Current.Response.Write(temp.Name + "<br/>");

Step 8: Output of Complex Data Type


Thanks for reading this article. Have a nice day.