C# 使用字典对对象进行LINQ分组
我有以下对象列表:C# 使用字典对对象进行LINQ分组,c#,linq,C#,Linq,我有以下对象列表: public class Record { public string SectionId { get; set; } public string SectionType { get; set; } public Dictionary<string, List<object>> RecordValues { get; set; } } var records = new List<Record> {
public class Record
{
public string SectionId { get; set; }
public string SectionType { get; set; }
public Dictionary<string, List<object>> RecordValues { get; set; }
}
var records = new List<Record>
{
new Record
{
SectionId = "1",
SectionType = "H",
RecordValues = new Dictionary<string, List<object>>
{
{ "1", new List<object> { "Item 1", "Item 2" } }
}
},
new Record
{
SectionId = "1",
SectionType = "H",
RecordValues = new Dictionary<string, List<object>>
{
{ "2", new List<object> { "Item 3", "Item 4" } }
}
},
new Record
{
SectionId = "2",
SectionType = "T",
RecordValues = new Dictionary<string, List<object>>
{
{ "1", new List<object> { "Item 5", "Item 6" } },
}
},
new Record
{
SectionId = "2",
SectionType = "T",
RecordValues = new Dictionary<string, List<object>>
{
{ "2", new List<object> { "Item 7", "Item 8" } }
}
}
}.AsEnumerable();
公共类记录
{
公共字符串SectionId{get;set;}
公共字符串节类型{get;set;}
公共字典记录值{get;set;}
}
var记录=新列表
{
新纪录
{
SectionId=“1”,
SectionType=“H”,
RecordValues=新字典
{
{“1”,新清单{“项目1”,“项目2”}
}
},
新纪录
{
SectionId=“1”,
SectionType=“H”,
RecordValues=新字典
{
{“2”,新清单{“项目3”,“项目4”}
}
},
新纪录
{
SectionId=“2”,
SectionType=“T”,
RecordValues=新字典
{
{“1”,新的清单{“项目5”,“项目6”},
}
},
新纪录
{
SectionId=“2”,
SectionType=“T”,
RecordValues=新字典
{
{“2”,新清单{“项目7”,“项目8”}
}
}
}.AsEnumerable();
我需要从上面的列表中创建另一个按节分组的列表。因此,我尝试了以下代码,但仍坚持选择Dictionary(请参见带???的行)
公共类部分
{
公共字符串Id{get;set;}
公共字符串类型{get;set;}
公共字典记录{get;set;}
}
var sections=records.GroupBy(g=>new{g.SectionId,g.SectionType})
.选择(s=>新建部分
{
Id=s.Key.SectionId,
类型=s.Key.SectionType,
Records=s.Select(x=>x.RecordValues).ToDictionary()/???
}).ToList();
分配记录属性的正确代码是什么?谢谢
编辑
我在上面的代码中犯了记录列表初始化错误(现在更改)。
记录对象中的每个RecordValues字典只包含一个键值对。
因此,我们的目标是按节对记录值进行分组,这样Linq分组的结果应该如下所示:
var sections = new List<Section>
{
new Section
{
Id = "1",
Type = "H",
Records = new Dictionary<string, List<object>>
{
{"1", new List<object> {"Item 1", "Item 2"}},
{"2", new List<object> {"Item 3", "Item 4"}}
}
},
new Section
{
Id = "2",
Type = "T",
Records = new Dictionary<string, List<object>>
{
{"1", new List<object> {"Item 5", "Item 6"}},
{"2", new List<object> {"Item 7", "Item 8"}}
}
}
};
var节=新列表
{
新章节
{
Id=“1”,
Type=“H”,
记录=新字典
{
{“1”,新的清单{“项目1”,“项目2”},
{“2”,新清单{“项目3”,“项目4”}
}
},
新章节
{
Id=“2”,
Type=“T”,
记录=新字典
{
{“1”,新的清单{“项目5”,“项目6”},
{“2”,新清单{“项目7”,“项目8”}
}
}
};
使用选择多个
和另一个分组方式
:
var sections = records.GroupBy(g => new { g.SectionId, g.SectionType })
.Select(s => new Section
{
Id = s.Key.SectionId,
Type = s.Key.SectionType,
Records = s.SelectMany(x => x.RecordValues)
.GroupBy(x => x.Key)
.ToDictionary(g => g.Key, g => g.SelectMany(x => x.Value).ToList())
}).ToList();
如果您确定所有合并记录中只有一项具有给定密钥,则可以使用以下命令:
var sections = records.GroupBy(g => new { g.SectionId, g.SectionType })
.Select(s => new Section
{
Id = s.Key.SectionId,
Type = s.Key.SectionType,
Records = s.SelectMany(x => x.RecordValues).ToDictionary(g => g.Key, g => g.Value)
}).ToList();
使用
SelectMany
和另一个GroupBy
:
var sections = records.GroupBy(g => new { g.SectionId, g.SectionType })
.Select(s => new Section
{
Id = s.Key.SectionId,
Type = s.Key.SectionType,
Records = s.SelectMany(x => x.RecordValues)
.GroupBy(x => x.Key)
.ToDictionary(g => g.Key, g => g.SelectMany(x => x.Value).ToList())
}).ToList();
如果您确定所有合并记录中只有一项具有给定密钥,则可以使用以下命令:
var sections = records.GroupBy(g => new { g.SectionId, g.SectionType })
.Select(s => new Section
{
Id = s.Key.SectionId,
Type = s.Key.SectionType,
Records = s.SelectMany(x => x.RecordValues).ToDictionary(g => g.Key, g => g.Value)
}).ToList();
使用
SelectMany
和另一个GroupBy
:
var sections = records.GroupBy(g => new { g.SectionId, g.SectionType })
.Select(s => new Section
{
Id = s.Key.SectionId,
Type = s.Key.SectionType,
Records = s.SelectMany(x => x.RecordValues)
.GroupBy(x => x.Key)
.ToDictionary(g => g.Key, g => g.SelectMany(x => x.Value).ToList())
}).ToList();
如果您确定所有合并记录中只有一项具有给定密钥,则可以使用以下命令:
var sections = records.GroupBy(g => new { g.SectionId, g.SectionType })
.Select(s => new Section
{
Id = s.Key.SectionId,
Type = s.Key.SectionType,
Records = s.SelectMany(x => x.RecordValues).ToDictionary(g => g.Key, g => g.Value)
}).ToList();
使用
SelectMany
和另一个GroupBy
:
var sections = records.GroupBy(g => new { g.SectionId, g.SectionType })
.Select(s => new Section
{
Id = s.Key.SectionId,
Type = s.Key.SectionType,
Records = s.SelectMany(x => x.RecordValues)
.GroupBy(x => x.Key)
.ToDictionary(g => g.Key, g => g.SelectMany(x => x.Value).ToList())
}).ToList();
如果您确定所有合并记录中只有一项具有给定密钥,则可以使用以下命令:
var sections = records.GroupBy(g => new { g.SectionId, g.SectionType })
.Select(s => new Section
{
Id = s.Key.SectionId,
Type = s.Key.SectionType,
Records = s.SelectMany(x => x.RecordValues).ToDictionary(g => g.Key, g => g.Value)
}).ToList();
您想在records属性中添加什么内容?每个节点的
记录值的全部内容?对于所有节点?记录是什么类型的?那么,根据您的示例,输出应该是什么样的呢?我已经编辑了我的问题并添加了所需的输出。您想在Records属性中添加什么?每个节点的记录值的全部内容?对于所有节点?记录是什么类型的?那么,根据您的示例,输出应该是什么样的呢?我已经编辑了我的问题并添加了所需的输出。您想在Records属性中添加什么?每个节点的记录值的全部内容?对于所有节点?记录是什么类型的?那么,根据您的示例,输出应该是什么样的呢?我已经编辑了我的问题并添加了所需的输出。您想在Records属性中添加什么?每个节点的记录值的全部内容?对于所有节点?记录是什么类型的?那么,根据您的示例,输出应该是什么样的呢?我已经编辑了我的问题并添加了理想的输出。感谢您的回答MarcinJuraszek。我已经编辑了我的问题。@Havrl我的解决方案会返回您想要的结果,不是吗?谢谢您的回答MarcinJuraszek。我已经编辑了我的问题。@Havrl我的解决方案会返回您想要的结果,不是吗?谢谢您的回答MarcinJuraszek。我已经编辑了我的问题。@Havrl我的解决方案会返回您想要的结果,不是吗?谢谢您的回答MarcinJuraszek。我已经编辑了我的问题。@Havrl我的解决方案会返回您想要的结果,不是吗?