我有班级人物,城市和街道。给它的门牌编号。我遇到过这样一个问题,一个人不仅可以住在一个城市,而且可以住在两个城市(纽约和洛杉矶)。如何给人添加新的城市。
var live = new List<Live>();
lives.Add(new Live
{
Person = new Person
{
FirstName = "Joe",
LastName = "Doe"
},
City = new City
{
Name = "NY",
},
Number = 31
});
发布于 2018-08-09 01:21:33
您可以将City更改为Cities (City列表)。
var live = new List<Live>();
lives.Add(new Live
{
Person = new Person
{
FirstName = "Joe",
LastName = "Doe"
},
Cities = new List<City>
{
new City
{
Name = "NY",
},
new City
{
Name = "LA",
}
},
Number = 31
});
为了进行编译,您的Person类应该定义如下:
public class Live
{
public Person Person { get; set; }
public List<City> Cities { get; set; }
public int Number;
}
此外,根据您的评论,如果您希望为每个城市使用Number
,那么我能想到的最简单的解决方案就是将数字从Person类移动到City类。
归根结底,这取决于你需要准确地建模什么,还有其他方法。
https://stackoverflow.com/questions/51752220
复制相似问题