2016-11-15 167 views
0

是否可以創建IEnumerable變量,將另一個列表的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"; 
} 
+1

相關文檔:http://stackoverflow.com/documentation/c%23/68/linq-queries/303/select-transforming-elements –

回答

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