.net - 任何用于逆透视数据的 .Net 函数或 linq 查询

标签 .net linq unpivot

是否有任何 .net 库可用于 unpivot excel 数据?我目前正在使用 LinqToExcel 框架从电子表格中读取数据,因此不确定是否有动态 linq 查询可用于执行逆透视。感谢您的任何建议。顺便说一句,我正在寻找一种可以处理多列的解决方案。

例子
原表

Product Location  Customer1   Customer2   Customer3
  A        X          10         20         100

目的地表
Product Location Customer    Demand
  A        X      Customer1    10
  A        X      Customer2    20
  A        X      Customer3    100

最佳答案

尝试这样的事情:

var customer1 =
    from c in excel.Worksheet()
    select new
    {
        Product = c["Product"],
        Location = c["Location"],
        Customer = "Customer1",
        Demand = c["Customer1"],
    };

var customer2 =
    from c in excel.Worksheet()
    select new
    {
        Product = c["Product"],
        Location = c["Location"],
        Customer = "Customer2",
        Demand = c["Customer2"],
    };

var customer3 =
    from c in excel.Worksheet()
    select new
    {
        Product = c["Product"],
        Location = c["Location"],
        Customer = "Customer3",
        Demand = c["Customer3"],
    };

var customers = customer1.Union(customer2).Union(customer3);

根据您的评论试试这个:
var columns = 4; /* how many columns to unpivot */
var customers =
    from n in Enumerable.Range(2, columns)
    from c in excel.Worksheet()
    select new
    {
        Product = c["Product"].Value,
        Location = c["Location"].Value,
        Column = n.ToString(),
        Demand = c[n].Value,
    };

关于.net - 任何用于逆透视数据的 .Net 函数或 linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11639687/

相关文章:

.net - 如何判断 VB.net 中的网页何时更改了 x%?

c# - Lambda 表达式 LINQ 中的条件 SELECT

.net - Linq:获取DataContext中所有表的列表

c# - 将字符串转换为 long 类型并在 asp.net MVC 中的 linq 查询中使用

google-apps-script - 如何在 Google Apps 脚本中转置和拆分?

sql - 交叉应用性能不佳,转换为 unpivot(或其他)?

C#:从 IEnumerable<IEnumerable<T>> 获取不同的 T

.net - 在调试器中格式化第 3 方 .NET 对象

sql - SQL Server 如何对多列进行 UnPivot

.net - .Net 的 Web 应用程序测试(WatiN 测试记录器)