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#.
- 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.
Now if we check the Lookup by right clicking and going tothe definition we didn't find any constructor defined in the Lookup class.

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

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


Dictionary Lookup
Now here I wrote a program where I have some duplicate values.
Firstly, I am using Lookup.
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.- static void Main(string[] args)
- {
- List < Employee > li = new List < Employee > ();
- li.Add(new Employee
- {
- Id = 1, age = 19, name = "Ritesh", gender = "M"
- });
- li.Add(new Employee
- {
- Id = 2, age = 20, name = "sujit", gender = "M"
- });
- li.Add(new Employee
- {
- Id = 3, age = 23, name = "Kabir", gender = "F"
- });
- li.Add(new Employee
- {
- Id = 4, age = 3, name = "mantu", gender = "F"
- });
- li.Add(new Employee
- {
- Id = 5, age = 24, name = "Kamlesh", gender = "M"
- });
- li.Add(new Employee
- {
- Id = 6, age = 28, name = "Manoj", gender = "M"
- });
- li.Add(new Employee
- {
- Id = 6, age = 28, name = "xxxx", gender = "M"
- });
- var res = li.ToLookup(x => x.Id);
- }


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:

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

- In a LookUp we can print the key values as follows.
And for dictionary we have different way to print the keys, one of these is:- var details = li.ToLookup(x => x.Id);
- foreach(var s in details)
- {
- Console.WriteLine(s.Key);
- }
- var result = li.ToDictionary(x => x.Id, x => x.name);
- foreach(KeyValuePair < int, string > s in result)
- {
- Console.WriteLine(s.Key);
- }
- 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.
- 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:
- static void Main(string[] args)
- {
- List<Employee> li = new List<Employee>();
- li.Add(new Employee { Id = 1, age = 19, name = "Ritesh", gender = "M" });
- li.Add(new Employee { Id = 2, age = 20, name = "sujit", gender = "M" });
- li.Add(new Employee { Id = 3, age = 23, name = "Kabir", gender = "F" });
- li.Add(new Employee { Id = 4, age = 3, name = "mantu", gender = "F" });
- li.Add(new Employee { Id = 5, age = 24, name = "Kamlesh", gender = "M" });
- li.Add(new Employee { Id = 6, age = 28, name = "Manoj", gender = "M" });
- }
- var result = li.ToLookup(x => x.Id > 4);

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.

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

- Printing each element as per key in LookUp:
And for Dictionary we can print directly the value as follows:- var res = li.ToLookup(x => x.Id);
- foreach(var p in res[5])
- {
- Console.WriteLine(p.name);
- }
- Console.WriteLine(res1[3]);
These are the few differences I noted about this Lookup and dictionary. Please comment if you found some more.

Richard LLLPosted Oct 4, 2018, 2:21 PM
Greate Explanation,Thanks
Prashanth KumarPosted Mar 29, 2018, 5:05 AM
Great article thank you
Shai AharoniPosted Dec 7, 2017, 1:45 AM
Thanks, nice article
Sonu ChaudharyPosted May 26, 2016, 10:32 AM
good one
Vivek KumarPosted Apr 28, 2016, 3:10 PM
Good one
Monalisha DashPosted Apr 15, 2016, 9:31 PM
Nice to know about Lookup.......great article......
Asfend YarPosted Mar 3, 2016, 3:34 PM
nice
Jaco ZwartsPosted Feb 11, 2016, 12:08 PM
Nice Article thank you
Mohsin AzamPosted Feb 8, 2016, 6:15 AM
nice one
Sibeesh VenuPosted Feb 8, 2016, 12:10 AM
Nice Share
Santhakumar MunuswamyPosted Feb 7, 2016, 1:09 PM
Thanks for good article
Kumaresh RajalingamPosted Feb 7, 2016, 7:10 AM
Good one sir
Ehsan SajjadPosted Feb 7, 2016, 4:34 AM
Good article
Vignesh ManiPosted Feb 6, 2016, 6:10 PM
Nice