[英]Add object to ICollection and then Add ICollection to a List<> ASP.NET
我不确定这是否可行甚至完全有意义,但是我需要添加一个对象(我们将其称为Person
到ICollection List People
并将集合添加到List <>中,以使人集合在一个List <>,还将包含其他参数。 我不确定如何执行此操作,但是可以向您展示到目前为止我已草绘的内容。
public void addPeopleToList(string PersonId)
{
Person p = findPerson(PersonId); /*Method that takes the ID and
returns an object from another List*/
ICollection<People> ICollectionPeople; //Create the ICollection
ICollectionPeople.Add(p); //Add Person to Collection
List.Add(ICollectionPeople); //Add Collection to List
}
如果这种方法不是正确的方法,那么我愿意接受所有其他建议。
如果您只是简单地告诉我们您想要实现什么目标,可能会更容易,但是无论如何:
您需要为ICollectionPeople
分配一个值(也可能值得重命名并遵循命名约定)。 也许
ICollection<People> peopleCollection = new List<People>();
虽然您真的需要该显式类型吗? 您可以使用var
。
您需要创建要添加人员集合的列表的实例。 也许
var list = new List<People>();
最后,像这样使用AddRange()
list.AddRange(peopleCollection);
我认为您需要的是另一堂课。
public class PersonWithAttributes : Person {
// add attribute properties here
}
然后在你的代码上面你会改变List
为类型List<PersonWithAttributes>
和代替.Add
荷兰国际集团在收集你会打电话List.AddRange(ICollectionPeople)
之后,您需要遍历List项并添加您正在谈论的其他属性。
不确定,但看起来您想拥有一个List<List<People>>
..如果是这样,则您的代码缺少集合的初始化。 你需要改变一点
public void addPeopleToList(string PersonId)
{
Person p = findPerson(PersonId); /*Method that takes the ID and
returns an object from another List*/
List<Person> ICollectionPeople = new List<Person>(); //Create the
ICollectionPeople.Add(p); //Add Person to Collection
List<List<Person>> personLists = new List<List<Person>>()
personLists.Add(ICollectionPeople); //Add Collection to List
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.