Home > Backend Development > C++ > How Can I Group a List of Objects by a Shared Property Using LINQ?

How Can I Group a List of Objects by a Shared Property Using LINQ?

Susan Sarandon
Release: 2025-01-13 12:12:43
Original
970 people have browsed it

How Can I Group a List of Objects by a Shared Property Using LINQ?

Group a list of objects using LINQ

In object-oriented programming, it is often necessary to group objects according to sharing conditions. This can be done efficiently using LINQ (Language Integrated Query). Below we'll explore how to group a list of objects into a new grouped list that contains multiple object lists.

Question:

Consider a class representing a user:

<code class="language-csharp">public class User
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public int GroupID { get; set; }
}</code>
Copy after login

Suppose we have a list of users:

<code class="language-csharp">List<User> userList = new List<User>();
userList.Add(new User { UserID = 1, UserName = "UserOne", GroupID = 1 });
userList.Add(new User { UserID = 2, UserName = "UserTwo", GroupID = 1 });
userList.Add(new User { UserID = 3, UserName = "UserThree", GroupID = 2 });
userList.Add(new User { UserID = 4, UserName = "UserFour", GroupID = 1 });
userList.Add(new User { UserID = 5, UserName = "UserFive", GroupID = 3 });
userList.Add(new User { UserID = 6, UserName = "UserSix", GroupID = 3 });</code>
Copy after login

Our goal is to group users by their GroupID, resulting in a list of users grouped by group.

Solution:

LINQ provides a powerful grouping mechanism using the GroupBy method. The following code snippet demonstrates how to achieve the desired grouping:

<code class="language-csharp">var groupedUserList = userList
    .GroupBy(u => u.GroupID)
    .Select(grp => grp.ToList())
    .ToList();</code>
Copy after login

Code breakdown:

  1. GroupBy: This is the core of the grouping operation. It groups users by their GroupID, generating a list of groups where each group represents users who belong to the same group.
  2. Select: Since the output of GroupBy is a list of groups (denoted as IGrouping<int, User>), we need to select users from each group. We use ToList method in Select to convert each group into a list of users.
  3. ToList: Finally, we use ToList to convert the list of user lists back into a list.

groupedUserList now contains the required grouping: a list of user lists, where each inner list represents a user that belongs to the same group.

The above is the detailed content of How Can I Group a List of Objects by a Shared Property Using LINQ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template