动态添加类中的属性

时间:2017-06-02 08:58:45

标签: c# json

我有一个产品类列表,例如:

{  
   Name = "Product 1",
   Category = "TV",
   Region = "China"
},
{  
   Name = "Product 2",
   Category = "Watch",
   Region = "Germany"
},
{  
   Name = "Product 3",
   Category = "Smartphone",
   Region = "USA"
}

每年未分类的产品价格表(格式为产品,年份和数量),例如:

Product 1, 2016, $2000    
Product 2, 2016, $300
Product 1, 2017, $1800
Product 3, 2017, $500
Product 2, 2017, $290

我需要以价格展示产品 - 以显示每年的价格比较。最终结果应如下所示:

Name           Category      Year 2016   Year 2017
Product 1      TV              2000        1800
Product 2      Watch            300         290
Product 3      Smartphone        -          500

由于价格表每年都会扩展,因此我打算在课堂上保留一个字符串列表作为属性。

List<string> Headers = new List<string> {
    "Name",
    "Category",
    "Region"
};

foreach (string year in distinctYear)
{
    Headers.Add("Y" + year);
}

在此之前,我已经陷入困境,如何将它们转换为类,以便我可以像这样分配值:

{  
   Name = "Product 1",
   Category = "TV",
   Y2016 = 2000,
   Y2017 = 1800
},
{  
   Name = "Product 2",
   Category = "Watch",
   Region = "Germany",
   Y2016 = 300,
   Y2017 = 290
},
{  
   Name = "Product 3",
   Category = "Smartphone",
   Region = "USA",
   Y2017 = 500
}

非常感谢任何帮助或建议。

3 个答案:

答案 0 :(得分:2)

使用字典,其中键是年份,值是价格。

class Product
        {
            public string Name { get; set; }
            public string Category { get; set; }
            public string Region { get; set; }
            public IDictionary<int, decimal> PricePerYear { get; set; } 
                = new Dictionary<int, decimal>( );
        }

var prod = new Product
                   {
                       Name = "Product1",
                       Category = "TV",
                       Region = "China",
                   };
        prod.PricePerYear.Add( 2016, 2000 );
        prod.PricePerYear.Add( 2017, 4500 );

或(C#7) 让我们使用一些元组!根据Chris的评论,您还可以在创建期间初始化列表/字典。

class Product
{
    public string Name { get; set; }
    public string Category { get; set; }
    public string Region { get; set; }
    public IList<(int year, decimal amount)> PricePerYear { get; set; }
}


 var prod = new Product
                       {
                           Name = "Product1",
                           Category = "TV",
                           Region = "China",
                           PricePerYear = new List<(int year, decimal amount)>
                                          {
                                              (year: 2016, amount: 5000),
                                              (2017, 10000),
                                              (2018, 5000),
                                          }
                       };

            (int year, decimal price) = prod.PricePerYear.First( );
            Console.WriteLine( $"Year: {year} Price: {price}" );
            Console.ReadLine( );

答案 1 :(得分:1)

您可以使用列表来存储价格;例如,使用这两个类:

    class Product
    {
        public string Name { get; set; }
        public string Category { get; set; }
        public string Region { get; set; }
        public List<Price> Prices { get; set; }
    }

    class Price
    {
        public int Year { get; set; }
        public decimal Amount { get; set; }
    }

然后您可以按如下方式初始化Product实例:

    Product prod1 = new Product()
    {
        Name = "Product1",
        Category = "TV",
        Region = "China",
        Prices = new List<Price>()
        {
            new Price()
            {
                Year = 2016,
                Amount = 2000
            },
            new Price()
            {
                Year = 2017,
                Amount = 1800
            }
        }
    };

答案 2 :(得分:0)

我将ProductListPriceList类合并到字典中:

foreach (var item in ProductList)
{
    var dict = new Dictionary<string, object>();
    dict["Name"] = item.Name;
    dict["Category"] = item.Category;
    dict["Region"] = item.Region;
    foreach (var i in item.PriceList)
    {
        dict[i.Year] = i.Amount;
    }
    list.Add(dict);
}