任何.Net函数或linq查询以取消数据

时间:2012-07-24 21:36:10

标签: .net linq unpivot

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

实施例 原始表

Product Location  Customer1   Customer2   Customer3
  A        X          10         20         100

Destinaton表

Product Location Customer    Demand
  A        X      Customer1    10
  A        X      Customer2    20
  A        X      Customer3    100

1 个答案:

答案 0 :(得分:5)

尝试这样的事情:

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,
    };