Search code examples
arrays.netlinqselectarray-merge

How I can select two elements to one list with linq


Firstly I need to create string array that include elements came from different properties of one object.

At the below code MyDatas is IEnumerable, prop1 and prop2 are string.

var array = myDatas.Select(md => md.prop1).ToArray();
array.Concat(myDatas.Select(md => md.prop2).ToArray()); 

and I will use this array for a method paramater.

I want make it simple and use like this;

Method(myDatas.Select(md=>md.prop1).......ToArray()); 

How will be second query?


Solution

  • You could pass

    myDatas.SelectMany(x => new[]{x.prop1, x.prop2}).ToArray()