c sharp

LINQ GroupBy()

In C#, if you want to group all the similar values at one place, LINQ in C# provides GroupBy() method.

The LINQ GroupBy method is used to group the similar values in a specified attribute and place with all the similar elements.

Method Syntax:

input_source.GroupBy(element => element.attribute);

Query Syntax:

from iterator in input_source group iterator by iterator.attribute

Where, input_source is the data source(List) and iterator is used to iterate the elements present in input_source.

Example 1:

We will create a List that stores Food details and Group the values in the list based on different attributes.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
    //define the data for Food
   class Food  
    {  
        public int food_price { get; set; }  
        public string name { get; set; }  
        public int quantity { get; set; }  
        public string city { get; set; }
    }
    static public  void Main(){

       //create data
       Listfirst_list = new List();
       //add values
first_list.Add(new Food { food_price=300,name="vegtables",quantity=1,city="california" });
first_list.Add(new Food { food_price=34,name="fruits",quantity=4,city="california"});
first_list.Add(new Food { food_price=100,name="mushroom",quantity=2,city="nepal" });
first_list.Add(new Food { food_price=564,name="vegtables",quantity=10,city="guntur" });
first_list.Add(new Food { food_price=500,name="fruits",quantity=8,city="nepal" });
first_list.Add(new Food { food_price=764,name="pulses",quantity=10,city="guntur" });
first_list.Add(new Food { food_price=400,name="pulses",quantity=8,city="nepal" });

Console.WriteLine("--------------------------Actual List--------------------------");

       foreach (var value in first_list)  
            {  
                Console.WriteLine(value.food_price+"->"+value.name+"->"+value.quantity+"->"+value.city);  
            }  



Console.WriteLine("--------------------------LINQ GroupBy city--------------------------");

         //group the values in city attribute
         var result = first_list.GroupBy(element =>element.city);


         //display the grouped results using foreach loop
     foreach (var i in result)  
            {  
              foreach (var j in i)  
            {  
                Console.WriteLine(j.food_price+"->"+j.name+"->"+j.quantity+"->"+j.city);  
            }    
            }  

Console.WriteLine("--------------------------LINQ GroupBy name--------------------------");

         //group the values in name attribute
         var result2 = first_list.GroupBy(element => element.name);


         //display the grouped results using foreach loop
     foreach (var i in result2)  
            {  
              foreach (var j in i)  
            {  
                Console.WriteLine(j.food_price+"->"+j.name+"->"+j.quantity+"->"+j.city);  
            }    
            }  


}
}

Output:

Explanation:

1. So first, we created a list that had food details.

2. After that, we are grouping values in the city column. Finally, we are using for each loop to display the grouped values.

3. Lastly, we are grouping values in the name column then we are using for each loop to display the grouped values.

Example 2:

We will create a List that stores Food details and Group the values in the list based on different attributes using Query Syntax.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
    //define the data for Food
   class Food  
    {  
        public int food_price { get; set; }  
        public string name { get; set; }  
        public int quantity { get; set; }  
        public string city { get; set; }
    }
    static public  void Main(){

       //create data
       Listfirst_list = new List();
       //add values
first_list.Add(new Food { food_price=300,name="vegtables",quantity=1,city="california" });
first_list.Add(new Food { food_price=34,name="fruits",quantity=4,city="california"});
first_list.Add(new Food { food_price=100,name="mushroom",quantity=2,city="nepal" });
first_list.Add(new Food { food_price=564,name="vegtables",quantity=10,city="guntur" });
first_list.Add(new Food { food_price=500,name="fruits",quantity=8,city="nepal" });
first_list.Add(new Food { food_price=764,name="pulses",quantity=10,city="guntur" });
first_list.Add(new Food { food_price=400,name="pulses",quantity=8,city="nepal" });

Console.WriteLine("--------------------------Actual List--------------------------");

       foreach (var value in first_list)  
            {  
                Console.WriteLine(value.food_price+"->"+value.name+"->"+value.quantity+"->"+value.city);  
            }  



Console.WriteLine("--------------------------LINQ GroupBy city--------------------------");

         //group the values in city attribute
         var result = from iterator1 in first_list group iterator1 by iterator1.city;


         //display the grouped results using foreach loop
     foreach (var i in result)  
            {  
              foreach (var j in i)  
            {  
                Console.WriteLine(j.food_price+"->"+j.name+"->"+j.quantity+"->"+j.city);  
            }    
            }  

Console.WriteLine("--------------------------LINQ GroupBy name--------------------------");

         //group the values in name attribute
         var result2 = from iterator2 in first_list group iterator2 by iterator2.name;



         //display the grouped results using foreach loop
     foreach (var i in result2)  
            {  
              foreach (var j in i)  
            {  
                Console.WriteLine(j.food_price+"->"+j.name+"->"+j.quantity+"->"+j.city);  
            }    
            }  


}
}

Output:

Explanation:

1. First, we created a list that had food details.

2. After that, we are grouping values in the city column then we are using for each loop to display the grouped values.

3. Lastly, we are grouping values in the name column then we are using for each loop to display the grouped values.

Conclusion

The LINQ GroupBy method is used to group the similar values in a specified attribute and place all the similar elements using Method and Query. It group all the similar values at one place and we can return the grouped values using foreach loop.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain