Lookup In LINQ

While working today I found an interesting method in LINQ -- Lookup. As currently I am working On ASP.NET  Web API and web services. Dictionary and List are two important collections I use regularly.

Several times in my project I need to convert a list to dictionary, packing different elements in dictionary and return it to user as key, value pair in JSON to the user, etc. Today while working I found something different which is very similar to dictionary  -- LOOKUP.

As I found this ToLookUp method in LINQ it seems interesting to me.

Here I explain what this Lookup actually is and what are the main differences between Lookup and Dictionary in C#.

  1. We can create an object of a dictionary but we cannot create object of a lookup. In real time we only use this lookup to convert from one data type to another. For example, converting a list to Lookup, etc.

    This is what error I got while creating the object of a Lookup.

    error

    Now if we check the Lookup by right clicking and going tothe definition we didn't find any constructor defined in the Lookup class.

    namespace

    Now if we check the dictionary we can create the object of it and a constructor is defined in it as follows.

    dictionarys

  2. In a dictionary we have a key value pair and the key in a dictionary cannot be duplicated. But in a Lookup we can have multiple values with a single key.

    key value

    key

    Dictionary Lookup

    Now here I wrote a program where I  have some duplicate values.

    Firstly, I am using Lookup.
    1. static void Main(string[] args)  
    2. {  
    3.     List < Employee > li = new List < Employee > ();  
    4.     li.Add(new Employee  
    5.     {  
    6.         Id = 1, age = 19, name = "Ritesh", gender = "M"  
    7.     });  
    8.     li.Add(new Employee  
    9.     {  
    10.         Id = 2, age = 20, name = "sujit", gender = "M"  
    11.     });  
    12.     li.Add(new Employee  
    13.     {  
    14.         Id = 3, age = 23, name = "Kabir", gender = "F"  
    15.     });  
    16.     li.Add(new Employee  
    17.     {  
    18.         Id = 4, age = 3, name = "mantu", gender = "F"  
    19.     });  
    20.     li.Add(new Employee  
    21.     {  
    22.         Id = 5, age = 24, name = "Kamlesh", gender = "M"  
    23.     });  
    24.     li.Add(new Employee  
    25.     {  
    26.         Id = 6, age = 28, name = "Manoj", gender = "M"  
    27.     });  
    28.     li.Add(new Employee  
    29.     {  
    30.         Id = 6, age = 28, name = "xxxx", gender = "M"  
    31.     });  
    32.     var res = li.ToLookup(x => x.Id);  
    33. }  
    Here I have converted the list to Lookup;  the list contains values with duplicate ids, and in my lookup I have the id as the key, but still it will execute. This is because lookup can contain a duplicate key.

    values

    details

    Now I am writing another program with duplicate records and checking it with the dictionary. Before converting a list into a dictionary,  here I am specifying its syntax:

    indicate value

    Now after converting the list to dictionary I got the following error.

    exception

  3. In a LookUp we can print the key values as follows.
    1. var details = li.ToLookup(x => x.Id);  
    2. foreach(var s in details)  
    3. {  
    4.     Console.WriteLine(s.Key);  
    5. }  
    And for dictionary we have different way to print the keys, one of these is:
    1. var result = li.ToDictionary(x => x.Id, x => x.name);  
    2. foreach(KeyValuePair < intstring > s in result)  
    3. {  
    4.     Console.WriteLine(s.Key);  
    5. }  
  4. As we see there is no add, clear, remove methods inside the lookup, so we cannot perform these operations in Lookup, we can only convert one form of data to Lookup, but operations like add, remove, clear, etc. can be easily done inthe dictionary.

  5. We can group the elements in a lookup as well as in a dictionary. For each grouping, if the condition satisfies then these elements appear for the key TRUE. And for elements for which the condition do not satisfy, then these elements appear for the key FALSE.

    For a LookUP we can group as follows:
    1. static void Main(string[] args)    
    2. {    
    3.     List<Employee> li = new List<Employee>();    
    4.     li.Add(new Employee { Id = 1, age = 19, name = "Ritesh", gender = "M" });    
    5.     li.Add(new Employee { Id = 2, age = 20, name = "sujit", gender = "M" });    
    6.     li.Add(new Employee { Id = 3, age = 23, name = "Kabir", gender = "F" });    
    7.     li.Add(new Employee { Id = 4, age = 3, name = "mantu", gender = "F" });    
    8.     li.Add(new Employee { Id = 5, age = 24, name = "Kamlesh", gender = "M" });    
    9.     li.Add(new Employee { Id = 6, age = 28, name = "Manoj", gender = "M" });         
    10.   
    11. }   
    1. var result = li.ToLookup(x => x.Id > 4);  
    result

    Here those elements which satisfy the condition or those elements whose id is greater then four will appear in the key TRUE; else others appear as FALSE. It is the same as dictionary.

    For Dictionary we have to use GroupBy keyword to group like this.

    error

  6. We can check a lookup if a specific element is present or not using a contain method like this.
    1. var res = li.ToLookup(x => x.Id);    
    2. if(res.Contains(3))    
    3. {    
    4.     Console.WriteLine("key present");    
    5. }      
    But for Dictionary we have options to check the key as well as value shown as follows.

    check the Key

  7. Printing each element as per key in LookUp:
    1. var res = li.ToLookup(x => x.Id);    
    2. foreach(var p in res[5])    
    3. {    
    4.     Console.WriteLine(p.name);    
    5.   
    6. }    
    And for Dictionary we can print directly the value as follows:
    1. Console.WriteLine(res1[3]);  

These are the few differences I noted about this Lookup and dictionary. Please comment if you found some more.

Read more articles on LINQ:

Up Next
    Ebook Download
    View all
    Learn
    View all