首页 > 解决方案 > 在 Linq 中动态将行转换为列

问题描述

我想将行转换为列,实际上我可以如下实现静态行到列,但它只有在我事先知道行位置的情况下才有效,所以有人知道如何在 Linq 中动态地将行转换为列吗?

这是下表

ItemCode  LOC001    LOC002   LOC003     
 AAA      10         11        12 
 BBB      13         31        14
 CCC      15         18        0

这是静态代码:

var table=(from x in Mst_LocationItems

group x by x.ItemCode into gr
select new
       {
          ItemCode=gr.Key,
          LOC001 = gr.Where(x=>x.LocationID == "LOC001").Sum(x=>x.Reorder),
          LOC002 = gr.Where(x=>x.LocationID == "LOC002").Sum(x=>x.Reorder),
          LOC003 = gr.Where(x=>x.LocationID == "LOC003").Sum(x=>x.Reorder)
       }).ToList();

           table.Dump();

标签: c#.netlinq

解决方案


推荐阅读