Inline array combination - LINQ

I initialize the array of items in the 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 inline array in the initializer, 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 need to combine everything up to the original select statement?

+6
source share
1 answer

You can use Concat :

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

If the items can 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(); 
+6
source

Source: https://habr.com/ru/post/1014960/


All Articles