我有一个字典定义为
var dataDict = new Dictionary<String, List<RICData>>();
将RICData类定义为
class RICData
{
public string pubdate { get; set; }
public string settle { get; set; }
public int colorder { get; set; }
}
以下说明了字典dataDict包含的数据 -
"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")
我想要做的是按照日期对数据进行分组,然后按颜色顺序对数据进行分组,并将以下内容重新调整为以下内容
"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")
这是一些示例代码。我想我不确定如何对这些数据进行分组
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));
答案 0 :(得分:3)
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));