开发者

Initializing collections that contain nested collections

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜