👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Using LINQ Group By to group data

Using LINQ Group By to group data

linq

26 Articles

Improve

In this article, let's learn about how to use GroupBy in LINQ in .NET.

Note: If you have not done so already, I recommend you read the article on Simulating Left Outer Join using LINQ.

Table of Contents

  1. Introduction
  2. Using LINQ Group By to group collections
  3. Ordering Grouped Collections
  4. Filtering Grouped Collections
  5. Summary

Introduction

When working with collection, we can group data in them using LINQ GroupBy() method. This will group elements in collection and gives a sequence of IGrouping<key , Object> interface. Internally GroupBy() Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. GroupBy() is similar to group by in SQL.

LINQ GroupBy() is used to answer questions about collection such as

  • Grouping sales data by product, region, or date to get insights into sales trends and patterns.
  • Grouping employee data by department, job title, or location to analyze the distribution of employees across the organization.
  • Grouping inventory data by product category, supplier, or manufacturing date to track inventory levels and monitor stock turnover.
  • Grouping log data by error type, source, or severity to track and diagnose application errors.
  • Grouping financial data by transaction type, account, or date to monitor account activity and perform analysis on financial data.

Using LINQ Group By to group collections

GroupBy() is a very popular thing, a very common thing that you would do in many applications where organizing, summarizing, and analyzing data in a more meaningful way. We can group elements in sequences (arrays, lists, etc.) based on a key value. The result is a new sequence that contains elements with the matching key element and their associated collection values.

Code Sample - LINQ GroupBy Objects with Single Field

Demo - LINQ GroupBy Clause with Objects Demo

Let's try LINQ GroupBy with Objects

  • We have Product class with following properties - Id, Name
  • The tables is loaded with product collection.
  • The Key Expression here is the Product Color. GroupBy() will group Products return new sequence of IGrouping<Key, Product>.
  • Click on GroupBy Button to view the result.
  • Click on reset to try other combination
Id Name Color Price Size
1 Shirt Black 1000 18
2 Shirt Red 1500 28
3 Shirt Black 2000 38
4 Shirt Red 2500 48
5 Shirt Brown 3000 58
6 Shirt White 3500 68
Key Id Name Price Size

Ordering Grouped Collections

We can order the collections prior to grouping using the LINQ OrderBy. But if we want to order the collection after grouping we need to use into and select to make this happen in Query Syntax and select in Method Syntax.

Code Sample - LINQ GroupBy Objects and OrderBy with Key

Filtering Grouped Collections

We can filter the collections after grouping using the LINQ Where. This is similar to Having clause in SQL.

Code Sample - LINQ GroupBy Objects and Filtering with Where Clause

Summary

In this article we learn't how to group data in collections using GroupBy. This allows you to group elements in collection based on a specified key resulting in IGrouping<Key, Object> collection. All these can be used with any IEnumerable or IQueryable types.

  • Linq
  • GroupBy
  • Group