Milleu Milleu - 22 days ago 6
C# Question

Enumerator of a lists object property

Is it possible to create a

IEnumerable
variable that Points foreach element of another List to a specific Property?

//...
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";
}

Answer

You can do it via linq:

var sampleList = new List<classA>();

var innerList = sampleList.Select(x => x.innerObject) //This creates an IEnumerable of all innerObjects in the original list