如何重构List <t>的集合?

时间:2019-04-03 01:40:42

标签: c# asp.net

我想知道是否有更好的方法来缩短我的代码或将其重构为可维护或可用的代码。而不是执行List,然后如此命名。.我可以做些什么来改进代码,使之更有意义,而不要做很多诸如List之类的事情。是否有一个好的方法呢?将其创建为助手。

此代码的目的是每个月每天存储数据,并希望它们处于循环内。每天都有数据填充。

var outputPerDays = new List<OutputPerDay>();

            var day1 = new List<Pricing>();
            var day2 = new List<Pricing>();
            var day3 = new List<Pricing>();
            var day4 = new List<Pricing>();
            var day5 = new List<Pricing>();
            var day6 = new List<Pricing>();
            var day7 = new List<Pricing>();
            var day8 = new List<Pricing>();
            var day9 = new List<Pricing>();
            var day10 = new List<Pricing>();
            var day11 = new List<Pricing>();
            var day12 = new List<Pricing>();
            var day13 = new List<Pricing>();
            var day14 = new List<Pricing>();
            var day15 = new List<Pricing>();
            var day16 = new List<Pricing>();
            var day17 = new List<Pricing>();
            var day18 = new List<Pricing>();
            var day19 = new List<Pricing>();
            var day20 = new List<Pricing>();
            var day21 = new List<Pricing>();
            var day22 = new List<Pricing>();
            var day23 = new List<Pricing>();
            var day24 = new List<Pricing>();
            var day25 = new List<Pricing>();
            var day26 = new List<Pricing>();
            var day27 = new List<Pricing>();
            var day28 = new List<Pricing>();
            var day29 = new List<Pricing>();
            var day30 = new List<Pricing>();
            var day31 = new List<Pricing>();
 [Serializable]
    public class OutputPerDay
    {
        public List<Pricing> Day1 { get; set; }
        public List<Pricing> Day2 { get; set; }
        public List<Pricing> Day3 { get; set; }
        public List<Pricing> Day4 { get; set; }
        public List<Pricing> Day5 { get; set; }
        public List<Pricing> Day6 { get; set; }
        public List<Pricing> Day7 { get; set; }
        public List<Pricing> Day8 { get; set; }
        public List<Pricing> Day9 { get; set; }
        public List<Pricing> Day10 { get; set; }
        public List<Pricing> Day11 { get; set; }
        public List<Pricing> Day12 { get; set; }
        public List<Pricing> Day13 { get; set; }
        public List<Pricing> Day14 { get; set; }
        public List<Pricing> Day15 { get; set; }
        public List<Pricing> Day16 { get; set; }
        public List<Pricing> Day17 { get; set; }
        public List<Pricing> Day18 { get; set; }
        public List<Pricing> Day19 { get; set; }
        public List<Pricing> Day20 { get; set; }
        public List<Pricing> Day21 { get; set; }
        public List<Pricing> Day22 { get; set; }
        public List<Pricing> Day23 { get; set; }
        public List<Pricing> Day24 { get; set; }
        public List<Pricing> Day25 { get; set; }
        public List<Pricing> Day26 { get; set; }
        public List<Pricing> Day27 { get; set; }
        public List<Pricing> Day28 { get; set; }
        public List<Pricing> Day29 { get; set; }
        public List<Pricing> Day30 { get; set; }
        public List<Pricing> Day31 { get; set; }
    }

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

[Serializable]
public class OutputPerDay
{
    public List<string>[] _pricesPerDay = new List<string>[3];

    public OutputPerDay()
    {
        for(int index = 0; index < 3; index++)
        {
            _pricesPerDay[0] = new List<string>();
        }
    }

    public List<string> Day1 => _pricesPerDay[0];
    public List<string> Day2 => _pricesPerDay[1];
    public List<string> Day3 => _pricesPerDay[2];
}

答案 1 :(得分:0)

您可以从List派生您的课程。

[Serializable]
public class OutputPerDay : List<List<Pricing>>
{
    public OutputPerDay() : base()
    {
        for (int i = 0; i <= 31; i++)
        {
            this.Add(new List<Pricing>());
        }
    }
}

用法:

var outputPerDay = new OutputPerDay();
outputPerDay[1].Add(new Pricing()); // Add a pricing to day 1
outputPerDay[1].Add(new Pricing()); // Add another pricing to day 1
outputPerDay[2].Add(new Pricing()); // Add a pricing to day 2
Console.WriteLine(outputPerDay[1].Count); // Display how many pricings are in day 1.