是否可以创建一个IEnumerable
变量,将另一个List的foreach元素指向特定属性?
//...
SomeList<classA> MainList = new SomeList<classA>();
IEnumerable<string> InnerList = ??MainList??;
MainList.add(new classA());
Console.Writeline(InnerList.First());
// ->"Hello World"
//...
class classA{
public string innerObject = "Hello World";
}
答案 0 :(得分:1)
您可以通过linq:
来完成var sampleList = new List<classA>();
var innerList = sampleList.Select(x => x.innerObject) //This creates an IEnumerable of all innerObjects in the original list