开发者

Initializing collections that contain nested collections

开发者 https://www.devze.com 2023-02-28 20:06 出处:网络
How do I initialize a co开发者_如何学Cllection of \'Quote\' objects based on the class shown below where \'Quote\' would contain a collection of at least 5 \'Rate\' objects.

How do I initialize a co开发者_如何学Cllection of 'Quote' objects based on the class shown below where 'Quote' would contain a collection of at least 5 'Rate' objects.

    List<Quote> quotes = new List<Quote> {new Quote { Id = 1, (need 5 Rate objects in here) } }

public class Quote
{
    public int Id { get; set; }
    public List<Rate> Rates { get; set; }
}

public class Rate
{
    public int Id { get; set; }
    public string AccommodationType { get; set; }
    public decimal Price { get; set; }
}


List<Quote> quotes = new List<Quote> {
  new Quote { 
    Id = 1, 
    Rates = new List<Rate> {
      new Rate { Id = 1, ...},
      new Rate { Id = 2, ...},
      ...
    } 
  },
  ...
};


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


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

精彩评论

暂无评论...
验证码 换一张
取 消