tags:

views:

72

answers:

2

I've got a dictionary laid out like so:

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}}});

What I need is:

        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}}});

That is, the dictionary projected "sideways", with the name of each Series as the key in the new dictionary, with the key of the old dictionary used as the name of the series.

Here's the series class...

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

Sorry if I am not expressing this problem clearly, please ask any questions you'd like, and thanks in advance for any help...

EDITED TO ADD EXAMPLE

+2  A: 

Create a grouping and then select out the new keys and values to create a dictionary. Like this:

// 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}
                                }
                        }
                    }
            };
*/
Sam
I ended up using this one!
Chris McCall
+1  A: 

Try this:

    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()
        )
diceguyd30