【发布时间】:2013-10-24 09:05:04
【问题描述】:
如果我有像
这样的作者对象列表List<Author> authors = new List<Author>{
new Author { Id = 1, Name = "John Freeman"};
new Author { Id = 1, Name = "Adam Kurtz"};
};
这个例子实际上包含在返回作者列表的静态方法中。
在我的另一个对象内部有List<Author> 类型的属性Authors。
现在我想将列表中的第二作者分配给Authors 属性。
我以为我可以使用Authors = GetAuthors()[1].ToList();
但我无法访问索引指定作者的 ToList()。
澄清
private static List<Author> GetAuthors() return list of authors (example above).
var someObject = new SomeObject()
{
Authors = // select only Adam Kurtz author using index
// and assign to Authors property of type List<Author>
};
【问题讨论】:
-
不确定我是否理解您的问题您需要
Authors = new List<Author>(){someObject[1]};吗? -
我只需要实例化 new List
() 而不是指定索引指定作者,就像下面回答的那样