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.
- 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);
- }
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.
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.
- var details = li.ToLookup(x => x.Id);
- foreach(var s in details)
- {
- Console.WriteLine(s.Key);
- }
And for dictionary we have different way to print the keys, one of these is:
- 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.
- var res = li.ToLookup(x => x.Id);
- if(res.Contains(3))
- {
- Console.WriteLine("key present");
- }
But for Dictionary we have options to check the key as well as value shown as follows.
- Printing each element as per key in LookUp:
- var res = li.ToLookup(x => x.Id);
- foreach(var p in res[5])
- {
- Console.WriteLine(p.name);
-
- }
And for Dictionary we can print directly the value as follows:
- 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: