LINQ使用Dictionary对象进行分组

时间:2014-01-14 18:02:33

标签: 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>
        {
            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();

我需要创建另一个按照上面列表中的部分分组的列表。所以我尝试了下面的代码,但坚持选择Dictionary(参见带有???的行)。

public class Section
{
    public string Id { get; set; }
    public string Type { get; set; }
    public Dictionary<string, List<object>> Records { get; set; }
}

var sections = records.GroupBy(g => new {g.SectionId, g.SectionType})
            .Select(s => new Section
            {
                Id = s.Key.SectionId,
                Type = s.Key.SectionType,
                Records = s.Select(x => x.RecordValues).ToDictionary() // ???
            }).ToList();

分配Records属性的正确代码是什么?感谢。

修改

我在上面的代码中犯了错误,用于记录列表初始化(现在更改)。 Record对象中的每个RecordValues字典仅包含单个键值对。 因此,目标是按部分对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"}}
                }
            }
        }; 

1 个答案:

答案 0 :(得分:3)

使用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();