【问题标题】:Transposing the list based on property names in C#根据 C# 中的属性名称转置列表
【发布时间】:2017-03-29 13:24:41
【问题描述】:

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

    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

基本上,源列表的两项应根据参考源生成一个新的 6 行列表,每个属性都标为价格,它需要如下所示

名称 RefSource 小时值 拉马尔汤姆 1 1 拉马尔汤姆 2 101 拉马尔汤姆 3 104 拉马尔理查德 1 2 拉马尔 理查德 2 201 拉马尔 理查德 3 204

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

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    试试这个:

    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;
    

    这给了我:

    【讨论】:

    • 感谢您的回复并正确编辑我的问题,我是堆栈交换的新手,但是在您的回复中,您已硬编码小时值,但我需要小时值作为 Pricei 属性的后缀因此,对于属性 Price7 -7 的小时数应该是小时,同样对于属性 'Value'=price7 值应该使用提前谢谢
    • @NISHET - 您的对象必须具有固定数量的这些属性,对吧?所以对我来说,你必须以某种方式动态生成这些值是没有意义的。你能更详细地解释一下你在做什么吗?
    猜你喜欢
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 2016-08-19
    • 2020-03-14
    • 1970-01-01
    相关资源
    最近更新 更多