.NET HELP

C# Groupby (How It Works For Developers)

Published April 3, 2024
Share:

In C#, the GroupBy method is a powerful tool that organizes elements from a data source into groups based on a specified key. This method is part of LINQ (Language Integrated Query) and can be used to group items by a single property or multiple properties, making it invaluable for data analysis and manipulation. The GroupBy method simplifies complex data operations, allowing for efficient data organization and retrieval based on specific criteria. We'll discuss GroupBy and the IronPDF Library in this blog.

Basics of GroupBy

The essence of the GroupBy method lies in its ability to categorize elements of a given collection into groups according to a specified key. This key property determines how items are grouped. For instance, you can group a list of students by their age key value, creating clusters of students with the same age. Each group is represented by a key value and a collection of items that share that key. The key property can be any object, such as a string, a number, or even an anonymous object, providing flexibility in how data is grouped.

Using GroupBy with Method Syntax

In C#, there are two ways to apply the GroupBy method: method syntax and query syntax. Method syntax uses lambda expressions to define the grouping key and is a direct approach to applying the GroupBy operation.

Consider the following example using method syntax to group a list of students by their age:

public class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
}
public class Program
{
    public static void Main()
    {
        List<Student> studentList = new List<Student>
        {
            new Student { Name = "Alice", Age = 20 },
            new Student { Name = "Bob", Age = 20 },
            new Student { Name = "Charlie", Age = 21 }
        };
        var groupedResult = studentList.GroupBy(student => student.Age);
        foreach (var group in groupedResult)
        {
            Console.WriteLine($"Age Group: {group.Key}");
            foreach (var student in group)
            {
                Console.WriteLine($"Student Name: {student.Name}");
            }
        }
    }
}
public class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
}
public class Program
{
    public static void Main()
    {
        List<Student> studentList = new List<Student>
        {
            new Student { Name = "Alice", Age = 20 },
            new Student { Name = "Bob", Age = 20 },
            new Student { Name = "Charlie", Age = 21 }
        };
        var groupedResult = studentList.GroupBy(student => student.Age);
        foreach (var group in groupedResult)
        {
            Console.WriteLine($"Age Group: {group.Key}");
            foreach (var student in group)
            {
                Console.WriteLine($"Student Name: {student.Name}");
            }
        }
    }
}
Public Class Student
	Public Property Name() As String
	Public Property Age() As Integer
End Class
Public Class Program
	Public Shared Sub Main()
		Dim studentList As New List(Of Student) From {
			New Student With {
				.Name = "Alice",
				.Age = 20
			},
			New Student With {
				.Name = "Bob",
				.Age = 20
			},
			New Student With {
				.Name = "Charlie",
				.Age = 21
			}
		}
		Dim groupedResult = studentList.GroupBy(Function(student) student.Age)
		For Each group In groupedResult
			Console.WriteLine($"Age Group: {group.Key}")
			For Each student In group
				Console.WriteLine($"Student Name: {student.Name}")
			Next student
		Next group
	End Sub
End Class
VB   C#

C# GroupBy (How It Works For Developers): Figure 1 - Console output from the previous code example

The LINQ GroupBy method uses a lambda expression to group students by their Age key value. group.Key represents the age group, and the inner foreach loop iterates over each student within a group, printing their names.

Utilizing GroupBy with Query Syntax

Query syntax offers a more expressive way to perform grouping operations, resembling SQL-like queries. It's particularly useful when dealing with complex data transformations and multiple grouping criteria. Here's how you can achieve similar functionality as the previous example using query syntax:

public static void Main()
{
    List<Student> studentList = new List<Student>
    {
        new Student { Name = "Alice", Age = 20 },
        new Student { Name = "Bob", Age = 20 },
        new Student { Name = "Charlie", Age = 21 }
    };
    var groupedResult = from student in studentList
                        group student by student.Age into ageGroup
                        select new { Age = ageGroup.Key, Students = ageGroup };
    foreach (var group in groupedResult)
    {
        Console.WriteLine($"Age Group: {group.Age}");
        foreach (var student in group.Students)
        {
            Console.WriteLine($"Student Name: {student.Name}");
        }
    }
}
public static void Main()
{
    List<Student> studentList = new List<Student>
    {
        new Student { Name = "Alice", Age = 20 },
        new Student { Name = "Bob", Age = 20 },
        new Student { Name = "Charlie", Age = 21 }
    };
    var groupedResult = from student in studentList
                        group student by student.Age into ageGroup
                        select new { Age = ageGroup.Key, Students = ageGroup };
    foreach (var group in groupedResult)
    {
        Console.WriteLine($"Age Group: {group.Age}");
        foreach (var student in group.Students)
        {
            Console.WriteLine($"Student Name: {student.Name}");
        }
    }
}
Public Shared Sub Main()
	Dim studentList As New List(Of Student) From {
		New Student With {
			.Name = "Alice",
			.Age = 20
		},
		New Student With {
			.Name = "Bob",
			.Age = 20
		},
		New Student With {
			.Name = "Charlie",
			.Age = 21
		}
	}
	Dim groupedResult = From student In studentList
		Group student By student.Age Into ageGroup = Group
		Select New With {
			Key .Age = Age,
			Key .Students = ageGroup
		}
	For Each group In groupedResult
		Console.WriteLine($"Age Group: {group.Age}")
		For Each student In group.Students
			Console.WriteLine($"Student Name: {student.Name}")
		Next student
	Next group
End Sub
VB   C#

In this snippet, the query syntax groups students by Age, similar to the method syntax but with a different syntax that some find more readable.

Grouping By Multiple Keys and Properties

A more advanced use of the GroupBy method involves grouping data by multiple keys or properties. This technique allows for more detailed data analysis and categorization based on multiple values. By using anonymous objects or tuples, you can group items based on a combination of properties, providing a richer data structure for your applications.

Example: Grouping Students by Name and Age

Consider a scenario where you need to group students not only by their age key value but also by their name, to identify students with the same name and age key values in a list. This can be accomplished by grouping by an anonymous type containing both the name and age.

public static void Main()
{
    List<Student> studentList = new List<Student>
    {
        new Student { Name = "Alice", Age = 20 },
        new Student { Name = "Alice", Age = 21 },
        new Student { Name = "Bob", Age = 20 },
        new Student { Name = "Charlie", Age = 21 }
    };
    var groupedResult = studentList.GroupBy(student => new { student.Name, student.Age });
    foreach (var group in groupedResult)
    {
        Console.WriteLine($"Group Key: Name = {group.Key.Name}, Age = {group.Key.Age}");
        foreach (var student in group)
        {
            Console.WriteLine($"Student Name: {student.Name}, Age: {student.Age}");
        }
    }
}
public static void Main()
{
    List<Student> studentList = new List<Student>
    {
        new Student { Name = "Alice", Age = 20 },
        new Student { Name = "Alice", Age = 21 },
        new Student { Name = "Bob", Age = 20 },
        new Student { Name = "Charlie", Age = 21 }
    };
    var groupedResult = studentList.GroupBy(student => new { student.Name, student.Age });
    foreach (var group in groupedResult)
    {
        Console.WriteLine($"Group Key: Name = {group.Key.Name}, Age = {group.Key.Age}");
        foreach (var student in group)
        {
            Console.WriteLine($"Student Name: {student.Name}, Age: {student.Age}");
        }
    }
}
Public Shared Sub Main()
	Dim studentList As New List(Of Student) From {
		New Student With {
			.Name = "Alice",
			.Age = 20
		},
		New Student With {
			.Name = "Alice",
			.Age = 21
		},
		New Student With {
			.Name = "Bob",
			.Age = 20
		},
		New Student With {
			.Name = "Charlie",
			.Age = 21
		}
	}
	Dim groupedResult = studentList.GroupBy(Function(student) New With {
		Key student.Name,
		Key student.Age
	})
	For Each group In groupedResult
		Console.WriteLine($"Group Key: Name = {group.Key.Name}, Age = {group.Key.Age}")
		For Each student In group
			Console.WriteLine($"Student Name: {student.Name}, Age: {student.Age}")
		Next student
	Next group
End Sub
VB   C#

In this example, students are grouped by name and age using an anonymous type as the key. This results in groups where each unique combination of name and age is represented as a separate group, demonstrating the flexibility of GroupBy for complex grouping scenarios.

Extra GroupBy Information

Advanced GroupBy Usage

GroupBy becomes even more powerful when you need to group by multiple key values or when you want to perform additional operations on the grouped data, such as counting, filtering, or ordering. You can achieve this by combining GroupBy with other LINQ methods or by using anonymous types to group by multiple properties.

Deferred Execution in GroupBy

It's important to note that GroupBy utilizes deferred execution, processing items when iterated over by a given key. This means the grouping operation is not immediately executed when the GroupBy method is called. Instead, the execution is deferred until the grouped data is iterated over, such as in a foreach loop. This behavior is efficient because it allows for further query optimizations and modifications before the data is finally processed.

Introducing IronPDF to C# Projects

C# GroupBy (How It Works For Developers): Figure 2 - IronPDF webpage

IronPDF is a comprehensive library for C# that enables developers to create, manipulate, and convert PDF documents within .NET applications. This powerful tool offers a wide range of functionalities, from generating PDFs from HTML to editing existing PDF files and much more. IronPDF simplifies integrating PDF capabilities into your applications, making it a valuable asset for any project requiring PDF manipulation.

The key feature of IronPDF is its HTML to PDF conversion, ensuring that layouts and styles are preserved. It generates PDFs from web content, making it great for reports, invoices, and documentation. HTML files, URLs, and HTML strings can be converted to PDFs seamlessly.

using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        var renderer = new ChromePdfRenderer();

        // 1. Convert HTML String to PDF
        var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
        var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
        pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

        // 2. Convert HTML File to PDF
        var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
        var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
        pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

        // 3. Convert URL to PDF
        var url = "http://ironpdf.com"; // Specify the URL
        var pdfFromUrl = renderer.RenderUrlAsPdf(url);
        pdfFromUrl.SaveAs("URLToPDF.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        var renderer = new ChromePdfRenderer();

        // 1. Convert HTML String to PDF
        var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
        var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
        pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

        // 2. Convert HTML File to PDF
        var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
        var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
        pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

        // 3. Convert URL to PDF
        var url = "http://ironpdf.com"; // Specify the URL
        var pdfFromUrl = renderer.RenderUrlAsPdf(url);
        pdfFromUrl.SaveAs("URLToPDF.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Dim renderer = New ChromePdfRenderer()

		' 1. Convert HTML String to PDF
		Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"
		Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent)
		pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf")

		' 2. Convert HTML File to PDF
		Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file
		Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath)
		pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf")

		' 3. Convert URL to PDF
		Dim url = "http://ironpdf.com" ' Specify the URL
		Dim pdfFromUrl = renderer.RenderUrlAsPdf(url)
		pdfFromUrl.SaveAs("URLToPDF.pdf")
	End Sub
End Class
VB   C#

Generating PDF Reports from Grouped Data

Let's extend our previous examples of grouping students by age key value. After grouping the students, we'll use IronPDF to generate a PDF report that lists these groups along with the names of the students in each group.

using IronPdf;
using System;
using System.Collections.Generic;
using System.Linq;
public class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
}
public class Program
{
    public static void Main()
    {
        IronPdf.License.LicenseKey = "License";
        List<Student> studentList = new List<Student>
        {
            new Student { Name = "Alice", Age = 20 },
            new Student { Name = "Bob", Age = 20 },
            new Student { Name = "Charlie", Age = 21 },
            new Student { Name = "David", Age = 21 }
        };
        var groupedResult = studentList.GroupBy(student => student.Age);
        var htmlContent = "<h1>Student Report</h1>";
        foreach (var group in groupedResult)
        {
            htmlContent += $"<h2>Age Group: {group.Key}</h2><ul>";
            foreach (var student in group)
            {
                htmlContent += $"<li>{student.Name}</li>";
            }
            htmlContent += "</ul>";
        }
        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf(htmlContent);
        var outputPath = "StudentReport.pdf";
        pdf.SaveAs(outputPath);
        Console.WriteLine($"PDF report generated at {outputPath}");
    }
}
using IronPdf;
using System;
using System.Collections.Generic;
using System.Linq;
public class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
}
public class Program
{
    public static void Main()
    {
        IronPdf.License.LicenseKey = "License";
        List<Student> studentList = new List<Student>
        {
            new Student { Name = "Alice", Age = 20 },
            new Student { Name = "Bob", Age = 20 },
            new Student { Name = "Charlie", Age = 21 },
            new Student { Name = "David", Age = 21 }
        };
        var groupedResult = studentList.GroupBy(student => student.Age);
        var htmlContent = "<h1>Student Report</h1>";
        foreach (var group in groupedResult)
        {
            htmlContent += $"<h2>Age Group: {group.Key}</h2><ul>";
            foreach (var student in group)
            {
                htmlContent += $"<li>{student.Name}</li>";
            }
            htmlContent += "</ul>";
        }
        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf(htmlContent);
        var outputPath = "StudentReport.pdf";
        pdf.SaveAs(outputPath);
        Console.WriteLine($"PDF report generated at {outputPath}");
    }
}
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.Linq
Public Class Student
	Public Property Name() As String
	Public Property Age() As Integer
End Class
Public Class Program
	Public Shared Sub Main()
		IronPdf.License.LicenseKey = "License"
		Dim studentList As New List(Of Student) From {
			New Student With {
				.Name = "Alice",
				.Age = 20
			},
			New Student With {
				.Name = "Bob",
				.Age = 20
			},
			New Student With {
				.Name = "Charlie",
				.Age = 21
			},
			New Student With {
				.Name = "David",
				.Age = 21
			}
		}
		Dim groupedResult = studentList.GroupBy(Function(student) student.Age)
		Dim htmlContent = "<h1>Student Report</h1>"
		For Each group In groupedResult
			htmlContent &= $"<h2>Age Group: {group.Key}</h2><ul>"
			For Each student In group
				htmlContent &= $"<li>{student.Name}</li>"
			Next student
			htmlContent &= "</ul>"
		Next group
		Dim renderer = New ChromePdfRenderer()
		Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
		Dim outputPath = "StudentReport.pdf"
		pdf.SaveAs(outputPath)
		Console.WriteLine($"PDF report generated at {outputPath}")
	End Sub
End Class
VB   C#

In this example, we first group students by age using the GroupBy method. Then, we construct an HTML string, which the method returns, that formats this grouped data into a report, with headings for each age group and lists of student names under each group. IronPDF's ChromePdfRenderer class is then used to convert this HTML string into a PDF document. The resulting PDF is saved to a file, providing a neatly formatted report of students grouped by age.

Output

Here is the output PDF generated by the IronPDF:

C# GroupBy (How It Works For Developers): Figure 3 - Outputted PDF from the previous code example

Conclusion

C# GroupBy (How It Works For Developers): Figure 4 - IronPDF licensing plan

The GroupBy method in C# is a versatile and powerful tool for grouping data based on specified keys. Whether you prefer method syntax with lambda expressions or the more declarative query syntax, GroupBy enables you to organize complex data structures in a manageable and readable manner. By mastering GroupBy and other LINQ methods, you can significantly enhance your ability to manipulate and analyze data in your C# applications.

IronPDF provides a free trial for those looking to explore its features before committing to a purchase. For those ready to integrate it into their projects, licensing begins at $749, making it a worthwhile investment for professional-grade PDF manipulation and generation in C# applications.

< PREVIOUS
IndexOf C# (How It Works For Developers)
NEXT >
C# Imap (How It Works For Developers)

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 11,622,374 View Licenses >