LINQ | Grouping Operator | ToLooKUp

In LINQ, grouping operators pick the elements of the sequence or collection which contains common attributes and serve them in a group. Or in other words, we can say that the grouping operator returns the group of elements based on the given key. This group is held in a special type of collection, which implements the IGrouping<TKey, TElement> interface, where TKey is the key through which the group is created and TElement is the collection of elements which peers with the grouping key value. For example, a sequence contains 6 elements, i.e, Abc, cvd, Abc, Abc, ert, Por, now we want a group in which contain all the Abc present in that sequence. So Abc is the key through which we create another group that contains 3 Abc. As shown in the below image:

The Standard Query Operator contains two different types of grouping operators:



  1. GroupBy
  2. ToLookup

ToLookup Operator

The ToLookup operator is used to put the element in a LookUp<TKey, TElement> based on a key selector function. Here, LookUp<TKey, TElement> is a one-to-many dictionary. The working of the ToLookup operator is quite similar to the GroupBy operator.

Important Points:

  • It does not support query syntax in C# and VB.Net languages.
  • It support method syntax in both C# and VB.Net languages.
  • It only present in Enumerable class.
  • It is overloaded in four different types.
  • It throw ArgumentNullException if the source or keySelector is null.
  • It is implemented by using immediate execution.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to divide the employees in 
// groups according to their gender
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
  
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                    emp_hire_date = "12/3/2017", emp_salary = 20000},
  
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
  
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
  
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Query to divide the employees
        // in the groups according to 
        // their gender Using the 
        // ToLookup method
        var res = emp.ToLookup(e => e.emp_gender);
  
        foreach(var val in res)
        {
            // Here salary is the key value
            Console.WriteLine("Group By Gender: {0}", val.Key);
  
            // Display name of the employees
            foreach(Employee e in val)
            {
                Console.WriteLine("Employee Name: {0}", e.emp_name);
            }
        }
    }
}

chevron_right


Output:

Group By Gender: Female
Employee Name: Anjita
Employee Name: Soniya
Employee Name: Supriya
Employee Name: Anju
Group By Gender: Male
Employee Name: Rohit
Employee Name: Anil


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.