Home > Backend Development > C++ > How Can I Group Objects by a Key Using LINQ in C#?

How Can I Group Objects by a Key Using LINQ in C#?

Mary-Kate Olsen
Release: 2025-02-02 00:21:10
Original
775 people have browsed it

How Can I Group Objects by a Key Using LINQ in C#?

Use the linq button group object

In object -oriented programming, it is very useful according to public attributes or key group objects. This example discusses how to use Linq (Language Integration) according to the Personid group object and obtain a car list associated with everyone.

Suppose we have a class called Person:

<code class="language-csharp">class Person
{
    public int PersonID;
    public string Car; // 将car改为Car,符合C#命名规范
}</code>
Copy after login
We also have a list of Person objects:

<code class="language-csharp">List<Person> persons;</code>
Copy after login
This list may contain multiple instances with the same Personid. For example:

<code class="language-csharp">persons = new List<Person>()
{
    new Person { PersonID = 1, Car = "Ferrari" },
    new Person { PersonID = 1, Car = "BMW" },
    new Person { PersonID = 2, Car = "Audi" }
};</code>
Copy after login
Our goal is to group these objects according to Personid and obtain a list of all cars owned by everyone. The expected results are shown below:

<code class="language-csharp">class Result
{
    public int PersonID;
    public List<string> Cars;
}</code>
Copy after login
After the grouping, we will get:

<code class="language-csharp">// 示例结果,实际结果由LINQ查询生成
var results = new List<Result>()
{
    new Result { PersonID = 1, Cars = new List<string> { "Ferrari", "BMW" } },
    new Result { PersonID = 2, Cars = new List<string> { "Audi" } }
};</code>
Copy after login
For this reason, we can use Linq's groupby method:

<code class="language-csharp">var results = from p in persons
              group p.Car by p.PersonID into g
              select new Result { PersonID = g.Key, Cars = g.ToList() };</code>
Copy after login
In the groupby clause, we specify Personid as a group key and projected the CAR property. The generated sequence will be a set of keys associated with their respective cars.

or, you can use the lookup dictionary to achieve the same results:

<code class="language-csharp">var carsByPersonId = persons.ToLookup(p => p.PersonID, p => p.Car);</code>
Copy after login
This will create a dictionary, where the key is Personid, and the value is everyone's car list. To search for a specific car, you can use:

<code class="language-csharp">// 这将返回一个空序列,对于查找中不存在的personId
var carsForPerson = carsByPersonId[personId];</code>
Copy after login
By using these technologies, you can use C#and LINQ to effectively follow the public bonding objects and retrieve the list of related data in a simple and efficient way. The code example has been improved, which is more in line with the coding specifications of C#.

The above is the detailed content of How Can I Group Objects by a Key Using LINQ in C#?. 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