是否可以使用LINQ对数据进行数据透视?我想知道是否可以使用LINQ从下列布局中将数据枢轴:CustID | OrderDate | Qty
1 | 1/1/2008 | 100
2 | 1/2/2008 | 200
1 | 2/2/2008 | 350
2 | 2/28/2008 | 221
1 | 3/12/2008 | 250
2 | 3/15/2008 | 2150变成这样:CustID | Jan- 2008 | Feb- 2008 | Mar - 2008 |
1 | 100 | 350 | 250
2 | 200 | 221 | 2150
3 回答
慕尼黑的夜晚无繁华
TA贡献1864条经验 获得超6个赞
List<CustData> myList = GetCustData(); var query = myList .GroupBy(c => c.CustId) .Select(g => new { CustId = g.Key, Jan = g.Where(c => c.OrderDate.Month == 1).Sum(c => c.Qty), Feb = g.Where(c => c.OrderDate.Month == 2).Sum(c => c.Qty), March = g.Where(c => c.OrderDate.Month == 3).Sum(c => c.Qty) });
GroupBy
森林海
TA贡献2011条经验 获得超2个赞
var query = from c in myList group c by c.CustId into gcs let lookup = gcs.ToLookup(y => y.OrderDate.Month, y => y.Qty) select new { CustId = gcs.Key, Jan = lookup[1].Sum(), Feb = lookup[2].Sum(), Mar = lookup[3].Sum(), };
- 3 回答
- 0 关注
- 613 浏览
添加回答
举报
0/150
提交
取消