目前尚不清楚您想如何聚合或排列数据,但如果您想通过以下方式聚合它们,year
您可以这样做:
var query = modules.GroupBy( m => m.date)
.Select( g =>
new
{
y = g.Key,
xrate = g.FirstOrDefault( x=> x.xrate),
yrate = g.FirstOrDefault( x => x.yrate)
});
FirstOrDefault()
这将为您提供具有聚合函数的分组年份列表:
Year | xrate | yrate
-------+----------+--------
2013 50 65
2014 59 69
或者您可以按匿名类型分组:
.GroupBy( m =>
new
{
m.date,
m.xrate,
m.yrate
})
而不是 only m.date
,因为您的date
,xrate
和yrate
对于所有行都是相同的。
编辑:好吧,为了旋转这个模块列表,我找不到比这更短更容易的东西:
var query = Modules.GroupBy(m => m.ModuleValue)
.Select(g => new { ModuleValue = g.Key, Values = g });
Func<Module, bool> matchy2013 = m => m.Year == 2013;
Func<Module, bool> matchy2014 = m => m.Year == 2014;
Func<Module, bool> matchy2015 = m => m.Year == 2015;
Func<Module, bool> matchy2016 = m => m.Year == 2016;
IList<NewModule> PivotedModules = new List<NewModule>();
foreach (var item in query)
{
var xrateRow = new NewModule
{
ModuleValue = item.ModuleValue,
ValueType = "xrate",
y2013 = item.Values.Where(matchy2013).FirstOrDefault().xrate,
y2014 = item.Values.Where(matchy2014).FirstOrDefault().xrate,
y2015 = item.Values.Where(matchy2015).FirstOrDefault().xrate,
y2016 = item.Values.Where(matchy2016).FirstOrDefault().xrate
};
var yrateRow = new NewModule
{
ModuleValue = item.ModuleValue,
ValueType = "yrate",
y2013 = item.Values.Where(matchy2013).FirstOrDefault().yrate,
y2014 = item.Values.Where(matchy2014).FirstOrDefault().yrate,
y2015 = item.Values.Where(matchy2015).FirstOrDefault().yrate,
y2016 = item.Values.Where(matchy2016).FirstOrDefault().yrate
};
var cumrateRow = new NewModule
{
ModuleValue = item.ModuleValue,
ValueType = "cumrate",
y2013 = item.Values.Where(matchy2013).FirstOrDefault().cumrate,
y2014 = item.Values.Where(matchy2014).FirstOrDefault().cumrate,
y2015 = item.Values.Where(matchy2015).FirstOrDefault().cumrate,
y2016 = item.Values.Where(matchy2016).FirstOrDefault().cumrate
};
PivotedModules.Add(xrateRow);
PivotedModules.Add(yrateRow);
PivotedModules.Add(cumrateRow);
}
给它一个托盘。您应该添加一个新类:
public class NewModule
{
public string ModuleValue { get; set; }
public string ValueType { get; set; }
public int y2013 { get; set; }
public int y2014 { get; set; }
public int y2015 { get; set; }
public int y2016 { get; set; }
}
PivotedModules
应该包含以下数据:
Module | ValueType | 2013 | 2014 | 2015 | 2016
---------+------------+--------+--------+--------+------
No2_gft xrate 50 59 59 59
No2_gft yrate 65 69 69 65
No2_gft cumrate 458 458 458 458
No3_gft xrate 50 59 59 59
No3_gft yrate 65 69 69 69
No3_gft cumrate 458 458 458 458
No4_gft xrate 50 59 59 59
No4_gft yrate 65 69 69 65
No4_gft cumrate 458 458 458 458