Milleu Milleu - 11 months ago 48
C# Question

Enumerator of a lists object property

Is it possible to create a

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());
// ->"Hello World"

class classA{
public string innerObject = "Hello World";

Answer Source

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