根据C#中的属性名称转置列表

时间:2017-03-29 04:54:15

标签: c# linq

我有一个具有以下属性的类

    public class Curve
    {
        public string CurveName { get; set; }

        public string RefSource { get; set; }
        public int price1 { get; set; }
        public int price2 { get; set; }
        public int price3 { get; set; }

        public static List<Curve> GetCurveList()
        {
            List<Curve> data = new List<Curve>()
            {
                new Curve() { CurveName="Lamar", price1 = 1, price2 = 101,price3=104,RefSource="Tome" },
                new Curve() { CurveName="Lamar", price1 = 2, price2 = 201,price3=204,RefSource="Richard" }
    }

我需要它在另一个列表中转换为以下属性

curveName=lamar,refSource=richard ,hour=(name of the property of price1, price2,price3)taking integer of the last part of the enclosed property,value associated with the price property

curveName=lamar,refSource=Tom,hour=(name of the property of price1, price2,price3)taking integer of the last part of the enclosed property,value associated with the price property

基本上,源列表的两个项目应该根据refsource生成一个包含6行的新列表,每个属性标题为价格,它需要如下所示

Name    RefSource   Hour    Value
lamar   tom        1           1
lamar   tom        2         101
lamar   tom        3         104
lamar   richard    1          2
lamar   richard    2         201
lamar   richard    3         204

小时列是源列表中属性名称Price1price2price3的后缀。

1 个答案:

答案 0 :(得分:2)

试试这个:

var query =
    from curve in Curve.GetCurveList()
    from result in new []
    {
        new { curve.CurveName, curve.RefSource, Hour = 1, Value = curve.price1 },
        new { curve.CurveName, curve.RefSource, Hour = 2, Value = curve.price2 },
        new { curve.CurveName, curve.RefSource, Hour = 3, Value = curve.price3 },
    }
    select result;

这让我:

results