Initializing collections that contain nested collections

前端 未结 3 1897
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 18:13

How do I initialize a collection of \'Quote\' objects based on the class shown below where \'Quote\' would contain a collection of at least 5 \'Rate\' objects.

          


        
相关标签:
3条回答
  • 2021-01-27 18:28
    List<Quote> quotes = new List<Quote> {
      new Quote { 
        Id = 1, 
        Rates = new List<Rate> {
          new Rate { Id = 1, ...},
          new Rate { Id = 2, ...},
          ...
        } 
      },
      ...
    };
    
    0 讨论(0)
  • 2021-01-27 18:33

    Use a factory method to create such object graphs for you.

    0 讨论(0)
  • 2021-01-27 18:36
    List<Quote> quotes = new List<Quote> {
      new Quote { 
        Id = 1,
        Rates = new List<Rate> {
          new Rate {
            Id = 1,
            AccommodationType = "Whatever",
            Price = 0m
          },
          new Rate { ......
          }
        }
      }
    };
    
    0 讨论(0)
提交回复
热议问题