根据类将数据存储到列表中的列表中

时间:2013-10-25 10:35:48

标签: c# list object

假设我有以下课程:

public class name()
{ 
   public string FirstName{ get; set; }
   public string LastName{ get; set; }

   public List<ItemOwner> ItemOwners { get; set; }

   public name()
   {
      this.ItemOwners = new List<ItemOwner>();
   }
} 
public class ItemOwner()
{ 
   public string nameofcat { get; set; }
   public string nameofsomething { get; set; }

   public List<AddressofCat> AddressofCats{ get; set; }

   public ItemOwner()
   {
      this.AddressofCats = new List<AddressofCat>();
   }
} 
public class AddressofCat()
{ 
   public string streetaddress{ get; set; }
   public string country { get; set; }

} 

我已经调用了数据库,并已将填充的类作为对象接收。

 var returnedcatobject = //select from database

我现在希望在列表中添加一个额外的cataddress:

 var newcataddress = new AddressofCat();
 newcataddress.streetaddress = "Randomaddress";
 newcataddress.country = "Rhodesia";

现在我们有了一个新的cat地址列表,我想将它们添加到返回的catobject中。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

如果returncatobject的类型为List<AddressofCat>,那么就像这样:

 var newcataddress = new AddressofCat();
 newcataddress.streetaddress = "Randomaddress";
 newcataddress.country = "Rhodesia";

returnedcatobject.Add(newcataddress);