简体   繁体   English

Linq GroupBy并选择其他属性作为列表

[英]Linq GroupBy and select other attributes as list

How to group by Properties and select other properties as lists using LINQ? 如何使用LINQ按属性分组并选择其他属性作为列表?

I'm bad at explaining it so here's an example: 我不好解释,所以这里有个例子:

Lets say I have a list of C# objects that can be represented in JSON like this: 可以说我有一个可以用JSON表示的C#对象列表,如下所示:

[
    { "id":123, "name":"John", "carId":1, "carAge":3 },
    { "id":123, "name":"John", "carId":2, "carAge":4 },
    { "id":4, "name":"Mary", "carId":3, "carAge":3 },
    { "id":4, "name":"Mary", "carId":4, "carAge":3 },
    { "id":123, "name":"John", "carId":5, "carAge":3 }
]

Among all those objects only carId is unique. 在所有这些对象中,只有carId是唯一的。

I would like to group them by person details and join those details with connected car details. 我想按人员详细信息对它们进行分组,然后将这些详细信息与相连的汽车详细信息一起加入。

The end result would look like this but as c# objects: 最终结果看起来像这样,但作为c#对象:

[
    {"id":123, "name":"John",
        [
            {"carId":1, "carAge":3},
            {"carId":2, "carAge":4},
            {"carId":5, "carAge":3}
        ]
    },
    {"id":4, "name":"Mary",
        [
            {"carId":3, "carAge":3},
            {"carId":4, "carAge":3}
        ]
    }
]

Edit: 编辑:

I'm stuck after 我被困住了

list.GroupBy(x => new { x.Id, x.Name })

because I don't need any aggregate functions but only selection. 因为我不需要任何聚合函数,而只需要选择。

I apologize for not posting more code, this is a simplified version, the real thing has more filtering and convoluted domain specific terminology. 抱歉,我没有发布更多代码,这是一个简化的版本,真实的东西具有更多的过滤和复杂的领域特定术语。

Everything is done in memory - without DB to worry about. 一切都在内存中完成-无需担心DB。

for each section of the group _ by _ expression create an anonymous object with the fields you want. group _ by _表达式的每个部分创建一个具有所需字段的匿名对象。

With any of the following ways you will construct a nested collection for each of your keys - when using a GroupBy it is not just about aggregations but about operations on specific groups of data. 通过以下任何一种方式,您将为每个键构造一个嵌套集合-使用GroupBy它不仅涉及聚合,还涉及对特定数据组的操作。 One case of it is aggregation methods. 一种情况是聚合方法。

var result = from item in data
             group new { item.carId, item.carAge }  by new {item.id, item.name} into g
             select g;

Another way would be 另一种方式是

var result = from item in data
             group  by new {item.id, item.name} into g
             select new {
                 g.Key,
                 Data = g.Select(i => new {i.carId, i.carAge})
             };

Using method syntax as you tried you can use the following overload: 尝试使用方法语法可以使用以下重载:

var result = data.Group(k => new {k.id, k.name},
                        e => new {e.carId, e.carAge});

Or if not the just add a Select for projection: 或者,如果不是,只需添加“ Select投影”:

var result = data.Group(k => new {k.id, k.name})
                 .Select(g => new {
                     g.Key,
                     Data = g.Select(i => new {i.carId, i.carAge})
                 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM