1

我有以下课程:

class AssembledDTO
{
    int pid,
    int blockId,
    List<string> references
}

我正在尝试按以下方式对所有内容进行分组:

AssembledParts.GroupBy(entry => new
                        {
                            entry.PID,
                            entry.BlockId
                        }).
                        Select( (key , val)=> new AssembledDTO
                            {
                                BlockId = key.Key.BlockId,
                                PID = key.Key.PID,
                                References = val.
                            }) 

References我想获得references我分组的每个组的所有加在一起的列表。

我怎么能这样做?我在这里想念什么?

4

1 回答 1

2

SelectMany应该可以使结果变平。

AssembledParts.GroupBy(entry => new
                    {
                        entry.PID,
                        entry.BlockId
                    }).
                    Select(key => new AssembledDTO
                        {
                            BlockId = key.Key.BlockId,
                            PID = key.Key.PID,
                            References = key.SelectMany(v => v.references).ToList();
                        }) 
于 2013-05-27T11:54:30.303 回答