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 { ......
}
}
}
};
精彩评论