Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combing Array Inline - LINQ

Tags:

arrays

c#

linq

I am initialising and array from items in a list as follows:

MyArray[] Arrayitems = SomeOtherList
.Select(x => new MyArray[]
{
   ArrayPar1 = x.ListPar1,
}).ToArray()

I have a secondary List that i would like to add to the same array inline in the initialiser, something like this ():

    MyArray[] Arrayitems = SomeOtherList
    .Select(x => new MyArray[]
    {
       ArrayPar1 = x.ListPar1,
    }).ToArray()
   .Join(
    MyArray[] Arrayitems = SomeOtherListNo2
    .Select(x => new MyArray[]
    {
       ArrayPar1 = x.ListPar1,
    }).ToArray()
   );

Is this possible or will i have to combine everything before the initial select statement?

like image 504
Dan Hall Avatar asked Feb 14 '17 09:02

Dan Hall


Video Answer


1 Answers

You can use Concat:

MyArray[] Arrayitems = SomeOtherList.Concat(SomeOtherListNo2)
.Select(x => new MyArray()
{
   ArrayPar1 = x.ListPar1,
}).ToArray();

If items could be contained in both lists and you want them only once in your result, you can use Union:

MyArray[] Arrayitems = SomeOtherList.Union(SomeOtherListNo2)
.Select(x => new MyArray()
{
   ArrayPar1 = x.ListPar1,
}).ToArray();
like image 188
Sefe Avatar answered Oct 01 '22 21:10

Sefe