6

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?

2
  • Do you mean new MyArray()? new MyArray[] does not make sense with your given initializer and result type. Commented Feb 14, 2017 at 9:35
  • Thanks Sefe, you're right but it's just an psuedo example, the actual code would have just 'Arrayitems' that is initialised elsewhere. You other answer is exactly what i need, thanks. Commented Feb 14, 2017 at 9:58

1 Answer 1

6

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.