J J J J - 3 months ago 14
C# Question

C# merge multiple arrays to one array

I get x-Amount of

Arrays
out of an
jagged array
looking like this:

string[] sArray1 = {"FB1" , "FB(1)" , "V1.0"};
string[] sArray2 = {"FB1" , "FB(1)" , "" , "V1.0"};
string[] sArray3 = {"FB2" , "FB(2)" , "V5.2"}


and I want to merge them into one
Array
when

sArray1[0] == sArray2[0] && sArray1[1] == sArray2[1]


the merged
Array
should look like this

string[] sArray12 = {"FB1" , "FB(1)" , "V1.0" , "V1.0"};


I already tried it with
Array.CopyTo()
; and
Enumerable.Union<>
.
The problem with
Enumerable.Union<>
was that when field 3 and 4 had the same value only field 3 would have been written in the
Array
.
Is there a way so that both values get written? Or is there a better approach to my problem?

Answer

Maybe something like

if (sArray1[0] == sArray2[0] && sArray1[1] == sArray2[1])
{
   var sArray12 = sArray1.Concat(sArray2.Skip(2))
                         .Where(x => !string.IsNullOrEmpty(x)) //probably
                         .ToArray();
}