简体   繁体   中英

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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM