This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

LINQ Grouping Operator


In C#, you can use LINQ to group data and apply various operations on it. Here is a general example of how you can use GroupBy in LINQ.

Here's an overview of GroupBy operator.

GroupBy

Using GroupBy in LINQ is useful for several reasons when working with collections of data.

  1. Organizing Data - GroupBy helps you organize and categorize data into meaningful subsets based on a common key. For example, grouping employees by department or sales by month.
  2. Aggregation - It allows you to perform aggregate operations (e.g., counting, summing, averaging) within each group. For instance, you might want to find the total sales per region or the average test score per class.
  3. Data Transformation - GroupBy can be used to transform data into a different structure. For example, grouping data by a property and then projecting a new structure with aggregated information or calculated fields.
  4. Simplified Queries - It simplifies complex queries by breaking them down into smaller, more manageable parts. You can first group the data and then apply additional transformations or operations to each group.
  5. Improved Readability - It can make your queries more readable and maintainable. By grouping data and then processing each group, you can make the logic of your query clearer and more intuitive.

Using LINQ GroupBy with LINQ Query and Lambda

LINQ Query with GroupBy

To group the people list by LastName, you can use the following LINQ query:

var groupedByLastName = from person in people
group person by person.LastName into grouped
select new
{
   LastName = grouped.Key,
   People = grouped.ToList()
};

LINQ GroupBy with Lambda

To group the people list by LastName, you can use the following LINQ query:

var groupedByLastNameLambda = people.GroupBy(person => person.LastName)
    .Select(group => new
    {
        LastName = group.Key,
        People = group.ToList()
    });

To group the people list by LastName, you can use the following LINQ query:

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

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Program
{
    public static void Main()
    {
        List<Person> people = new List<Person>
        {
           new Person { FirstName = "Rohatash", LastName = "Kumar" },
               new Person { FirstName = "Mohit", LastName = "Singh" },
               new Person { FirstName = "Saurav", LastName = "Kumar" }
        };

        // Group by LastName using LINQ query 
        Console.WriteLine("LINQ Query Output:");
        Console.WriteLine("");
        var groupedByLastName = from person in people
                                group person by person.LastName into grouped
                                select new
                                {
                                    LastName = grouped.Key,
                                    People = grouped
                                };
        foreach (var group in groupedByLastName)
        {
            Console.WriteLine("Last Name:" + group.LastName);
            foreach (var person in group.People)
            {
                Console.WriteLine(person.FirstName);
            }
        }

        // Group by LastName using Lambda 
        Console.WriteLine("");
        Console.WriteLine("");
        Console.WriteLine("Lambda Query Output:");
        Console.WriteLine("");
        var groupedByLastNameLambda = people.GroupBy(person => person.LastName)
          .Select(group => new
          {
              LastName = group.Key,
              People = group.ToList()
          });

        foreach (var group in groupedByLastNameLambda)
        {
            Console.WriteLine("Last Name:" + group.LastName);
            foreach (var person in group.People)
            {
                Console.WriteLine(person.FirstName);
            }
        }
    }
}

Output

LINQ
Prev Next