c# - 在 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();
解决方案
推荐阅读
- openssl - 如何使用 openssl x509 为每个证书生成随机序列号?
- function - Powershell - 从变量调用子函数
- javascript - 将 png 转换为 gltf 3d 模型纹理 three.js 和 fabric.js
- android - 在 Android 上没有从 deeplink 和 Branch.io 获得推荐网站
- regex - 木偶正则表达式与变量名不匹配
- python - SyntaxError:在区块链上扫描字符串文字错误时 EOL
- git - Bitbucket:帮助需要将更新从分叉移动到上游
- python-3.x - 如何将secondary_y的线条与plotly express的默认y条对齐?
- vue.js - responsive-loader 只返回一个数据 url
- reactjs - 如何在打字稿中为领域对象中的可选字段声明类型?