使用Linq转换字典的问题

时间:2010-10-18 13:25:19

标签: c# linq

我有一本字典如下:

Dictionary<string, List<Series>> example = new Dictionary<string, List<Series>>();

        example.Add("Meter1",new List<Series>(){ new Series{ name="Usage", data = new double[] {1,2,3}},
            new Series{ name = "Demand", data= new double[]{4,5,6}}});

        example.Add("Meter2", new List<Series>(){ new Series{ name="Usage", data = new double[] {1,2,3}},
            new Series{ name = "Demand", data= new double[]{4,5,6}}});

我需要的是:

        Dictionary<string, List<Series>> exampleResult = new Dictionary<string, List<Series>>();

        exampleResult.Add("Usage", new List<Series>(){ new Series{ name="Meter1", data = new double[] {1,2,3}},
            new Series{ name = "Meter2", data= new double[]{1,2,3}}});

        exampleResult.Add("Demand", new List<Series>(){ new Series{ name="Meter1", data = new double[] {4,5,6}},
            new Series{ name = "Meter2", data= new double[]{4,5,6}}});

也就是说,字典以“横向”投影,每个系列的名称作为新字典中的键,并使用旧字典的键作为系列的名称。

这是系列课......

public class Series
{
    public string name { get; set; }
    public double[] data { get; set; }
}

对不起,如果我没有清楚地表达这个问题,请提出您想要的任何问题,并提前感谢您的帮助......

已编辑添加示例

2 个答案:

答案 0 :(得分:3)

创建分组,然后选择新的键和值以创建字典。像这样:

// source data
var d = new Dictionary<string, Series[]>
            {
                {
                    "key1", new[]
                                {
                                    new Series
                                        {
                                            name = "Usage", 
                                            data = new double[] {1, 2, 3}
                                        },
                                    new Series
                                        {
                                            name = "Demand", 
                                            data = new double[] {4, 5, 6}
                                        }
                                }
                    },
                {
                    "key2", new[]
                                {
                                    new Series
                                        {
                                            name = "Usage", 
                                            data = new double[] {1, 2, 3}
                                        },
                                    new Series
                                        {
                                            name = "Demand", 
                                            data = new double[] {4, 5, 6}
                                        }
                                }
                    }
            };

// transform
var y = (
            from outer in d
            from s in outer.Value
            let n = new
                        {
                            Key = s.name, 
                            Series = new Series
                                        {
                                            name = outer.Key, 
                                            data = s.data
                                        }
                        }
            group n by n.Key
            into g
            select g
        ).ToDictionary(g1 => g1.Key,
                       g2 => g2.Select(g3 => g3.Series).ToArray());


/* results:
var y = new Dictionary<string, Series[]>
            {
                {
                    "Usage",
                    new[]
                        {
                            new Series
                                {
                                    name = "key1", 
                                    data = new double[] { 1, 2, 3 }
                                },
                            new Series
                                {
                                    name = "key2", 
                                    data = new double[] { 1, 2, 3 }
                                }
                        }
                    },
                {
                    "Demand",
                    new[]
                        {
                            new Series
                                {
                                    name = "key1", 
                                    data = new double[] {4, 5, 6},
                                },
                            new Series
                                {
                                    name = "key2", 
                                    data = new double[] {4, 5, 6}
                                }
                        }
                    }
            };
*/

答案 1 :(得分:1)

试试这个:

    example
        .SelectMany(x => x.Value
            .Select(y => y.name)
        ).Distinct()
        .ToDictionary(
            x => x,
            x => example
            .SelectMany(y => y.Value
                .Where(z => z.name == x)
                .Select(z => new Series{ name = y.Key, data = z.data })
            ).ToList()
        )