Bookamp
Bookamp

Reputation: 682

Linq Group By statement on a Dictionary containing a list

I have a dictionary defined as

var dataDict = new Dictionary<String, List<RICData>>();

with the RICData class defined as

class RICData
{
        public string pubdate { get; set; }
        public string settle { get; set; }
        public int colorder { get; set; }
}

The following illustrates the data the dictionary dataDict contains -

"TEST1", ("12/01/2015, 100.1, 1", "12/02/2015, 200.1, 2", "12/03/2015, 300.4, 3")
"TEST2", ("12/01/2015, 150.1, 6", "12/02/2015, 200.1, 7")
"TEST3", ("12/01/2015, 250.1, 4", "12/02/2015, 400, 5")

What I would like to do is group the data by date and order by colorder and retun something simlar to what is below

"12/01/2015", ("TEST1, 100.1, 1", "TEST3, 250.1, 4", "TEST2, 150.1, 6")
"12/02/2015", ("TEST1, 200.1, 2", "TEST3, 400, 5", "TEST2, 200.1, 7"
"12/03/2015", ("TEST1, 300.4, 3")

Here's some sample code. I guess I'm not sure how to group this data

var dataDict = new Dictionary<String, List<RICData>>();
            var rdList = new List<RICData>();
            rdList.Add(new RICData{pubdate = "12/01/2015", settle = "100.1", colorder = 1});
            rdList.Add(new RICData{pubdate = "12/02/2015", settle = "110.1", colorder = 2});
            rdList.Add(new RICData { pubdate = "12/03/2015", settle = "120.1", colorder = 3 });
            dataDict.Add("TEST1", rdList);
            var rdList1 = new List<RICData>();
            rdList1.Add(new RICData { pubdate = "12/01/2015", settle = "140.1", colorder = 6 });
            rdList1.Add(new RICData { pubdate = "12/02/2015", settle = "100.1", colorder = 7 });            
            dataDict.Add("TEST2", rdList1);
            var rdList2 = new List<RICData>();
            rdList2.Add(new RICData { pubdate = "12/01/2015", settle = "240.1", colorder = 4 });
            rdList2.Add(new RICData { pubdate = "12/02/2015", settle = "200.1", colorder = 5 });
            dataDict.Add("TEST3", rdList2);    
            //?????
            var resultGrp = dataDict.GroupBy(p => p.Value.Select(x => x.pubdate));

Upvotes: 0

Views: 70

Answers (1)

Nikolai Samteladze
Nikolai Samteladze

Reputation: 7797

public class RICData
{
        public string PubDate { get; set; }
        public string Settle { get; set; }
        public int ColorDer { get; set; }
}

public class NewRICData
{
        public string Label { get; set; }
        public string Settle { get; set; }
        public int Colorder { get; set; }
}

var oldDict = new Dictionary<string, List<RICData>>();

var newDict = oldDict.SelectMany(pair => pair.Value.Select(data => new
    {
        PubDate = DateTime.Parse(data.PubDate), 
        NewRICData = new NewRICData
        {
            Label = pair.Key, 
            Settle = data.Settle, 
            ColorDer = data.ColorDer
        }
    }))
    .GroupBy(x => x.PubDate.Date)
    .ToDictionary(group => group.Key.ToString("d"), 
                  group => group.Select(x => x.NewRICData)
                                .OrderBy(x => x.ColorDer));

Upvotes: 3

Related Questions