如何使用LINQ将行转换为列?

时间:2016-05-13 17:52:38

标签: linq

如何将所有行转换为列?考虑第一列将使用LINQ生成列名。

计算产品是否与product1重复的总和:

我有一个包含以下数据的列表

   Product     Y1   Y2  Y3  Y4  Y5  Y6  Y7  Y8  Y9  Y10
   Product1    1    2   3   4   5   6   7   8   9   10
   Product1    2   3    4   5    6   7  8    9 10  11
   Product2    2    3   4   5   6   7   8   9   10  11
   Product3    3    4   5   6   7   8   9   10  11  12
   Product4    4    5   6   7   8   9   10  11  12  13

我需要将输出作为列表

YKey Year  Product1 Product2    Product3
Y1  2016    3   2   3   4
Y2  2017    5   3   4   5
Y3  2018    7   4   5   6
Y4  2019    9   5   6   7
Y5  2020    11  6   7   8
Y6  2021    13  7   8   9
Y7  2022    15  8   9   10
Y8  2023    17  9   10  11
Y9  2024    19  10  11  12
Y10 2025    21  11  12  3

我只有4种产品只是为了让您对我的要求有一个清晰的了解,我可能会在固定的20年内每年都有很多产品价值。

年份将从当年开始,如果当前2016年则y1 = 2016年,y2 = 2017年......

下面我提供了list<class>作为示例

  private List<Data> CreateColumnData()
    {
        var list = new List<Data>();

        list.Add(new Data() { ProductName = "Product1", Year1 = 1, Year2  = 2, Year3 = 3, Year4 = 4, Year5 = 5, Year6 = 6, Year7 = 7,Year8 = 8,Year9=9,Year10=10});
        list.Add(new Data() { ProductName = "Product2", Year1 = 2, Year2 = 3, Year3 = 4, Year4 = 5, Year5 = 6, Year6 = 7, Year7 = 8, Year8 = 9, Year9 = 10, Year10 = 11 });
        list.Add(new Data() { ProductName = "Product3", Year1 = 3, Year2 = 4, Year3 = 5, Year4 = 6, Year5 = 7, Year6 = 8, Year7 = 9, Year8 = 10, Year9 = 11, Year10 = 12 });
        list.Add(new Data() { ProductName = "Product4", Year1 = 4, Year2 = 5, Year3 = 6, Year4 = 7, Year5 = 8, Year6 = 9, Year7 = 10, Year8 = 11, Year9 = 12, Year10 = 13});

        return list;
    }
  public class Data
{
    public string ProductName { get; set; }
    public int Year1 { get; set; }
    public int Year2 { get; set; }
    public int Year3 { get; set; }
    public int Year4 { get; set; }
    public int Year5 { get; set; }
    public int Year6 { get; set; }
    public int Year7 { get; set; }
    public int Year8 { get; set; }
    public int Year9 { get; set; }
    public int Year10 { get; set; }
    public int Year11 { get; set; }
    public int Year12 { get; set; }
    public int Year13 { get; set; }
    public int Year14 { get; set; }
    public int Year15 { get; set; }
}

1 个答案:

答案 0 :(得分:0)

我认为Linq没有那么多工作,只有FirstOrDefault。

class Program
{
    static void Main()
    {
        List<Data> src = CreateColumnData();

        // Result is a List
        List<ResultRow> res = new List<ResultRow>();

        foreach (Data dd in src) {
            for (int i = 1; i <= 15; i++) 
            {
                String yk = "Y" + i.ToString();
                ResultRow r = res.FirstOrDefault(rr => rr.YK == yk);
                if (r == null)
                {
                    r = new ResultRow(yk, 2015 + i, 0, 0, 0, 0);
                    res.Add(r);
                }
                switch (dd.ProductName)
                {
                    case "Product1": r.Product1 += dd.getYear(i); break;
                    case "Product2": r.Product2 += dd.getYear(i); break;
                    case "Product3": r.Product3 += dd.getYear(i); break;
                    case "Product4": r.Product4 += dd.getYear(i); break;
                    default: break;
               }
            }          
        }

        res.All(rr => { 
            Console.WriteLine(rr.ToString());
            return true; });
        Console.ReadKey();
    }

    static private List<Data> CreateColumnData()
    {
        var list = new List<Data>();

        list.Add(new Data() { ProductName = "Product1", Year1 = 1, Year2 = 2, Year3 = 3, Year4 = 4, Year5 = 5, Year6 = 6, Year7 = 7, Year8 = 8, Year9 = 9, Year10 = 10 });
        list.Add(new Data() { ProductName = "Product2", Year1 = 2, Year2 = 3, Year3 = 4, Year4 = 5, Year5 = 6, Year6 = 7, Year7 = 8, Year8 = 9, Year9 = 10, Year10 = 11 });
        list.Add(new Data() { ProductName = "Product3", Year1 = 3, Year2 = 4, Year3 = 5, Year4 = 6, Year5 = 7, Year6 = 8, Year7 = 9, Year8 = 10, Year9 = 11, Year10 = 12 });
        list.Add(new Data() { ProductName = "Product4", Year1 = 4, Year2 = 5, Year3 = 6, Year4 = 7, Year5 = 8, Year6 = 9, Year7 = 10, Year8 = 11, Year9 = 12, Year10 = 13 });

        return list;
    }
}

// result POCO
public class ResultRow
{
    public string YK { get; set; }
    public int Year { get; set; } 
    public int Product1 { get; set; }
    public int Product2 { get; set; }
    public int Product3 { get; set; }
    public int Product4 { get; set; }

    public ResultRow (string YK, int Year, int Product1, int Product2, int Product3, int Product4)
    {
        this.YK = YK;
        this.Year = Year;
        this.Product1 = Product1;
        this.Product2 = Product2;
        this.Product3 = Product3;
        this.Product4 = Product4;
    }

    public override string ToString() {
        return string.Format("{0},{1},{2},{3},{4},{5}",
            YK, Year, Product1, Product2, Product3, Product4);
    }
}

public class Data
{
    public string ProductName { get; set; }
    public int Year1 { get; set; }
    public int Year2 { get; set; }
    public int Year3 { get; set; }
    public int Year4 { get; set; }
    public int Year5 { get; set; }
    public int Year6 { get; set; }
    public int Year7 { get; set; }
    public int Year8 { get; set; }
    public int Year9 { get; set; }
    public int Year10 { get; set; }
    public int Year11 { get; set; }
    public int Year12 { get; set; }
    public int Year13 { get; set; }
    public int Year14 { get; set; }
    public int Year15 { get; set; }

    public int getYear(int i) {
        switch (i) {
            case 1: return Year1; 
            case 2: return Year2;
            case 3: return Year3; 
            case 4: return Year4; 
            case 5: return Year5; 
            case 6: return Year6;
            case 7: return Year7; 
            case 8: return Year8; 
            case 9: return Year9; 
            case 10: return Year10; 
            case 11: return Year11; 
            case 12: return Year12; 
            case 13: return Year13; 
            case 14: return Year14; 
            case 15: return Year15; 
            default: return 0;
        }
    }
}
相关问题