views:

79

answers:

3

How can i combine two arrays in to a single array during compound selection ( without using Union ) ( The question was asked at interview).

    var num1 = new int[] { 12, 3, 4, 5 };
    var num2 = new int[] { 1, 33, 6, 10 };

I tried as

    var pairs = from a in num1 from b in num2  select new {combined={a,b}};

Expected: combined need to be {12,3,4,5,1,33,6,10}

+5  A: 
num1.Concat( num2 );

I'm not sure if there is a related LINQ keyword.

Tinister
+5  A: 

If you just want to combine 2 arrays into a new array which contains elements from both arrays then use concat.

var combined = num1.Concat(num2);
var combinedAsArray = combined.ToArray();
JaredPar
Thank you very much.Tinister answered first ,May i tick his answer ?
@linqfying tick the answer you feel is best. Tinister beat me to the punch so I would tick his.
JaredPar
Thanks for your openminded reply :)great!
A: 

var newArray = (from number in num1.Concat(num2) select number).ToArray();

Greg Andora
Thank you very much Greg
The linq construct does not add anything. 'num1.Concat(num2).ToArray()' is enough.
Johan Kullbom
I agree it doesn't, but his question asked for a compound selection, which to me would imply a linq construct as opposed to just calling a linq extension method by itself.
Greg Andora