ib11
ib11

Reputation: 2558

Visual C# how to merge two 1D string[] arrays into one 2D string[,] array

I have been diligently studying the help file and all similar questions, but was not able to get to a simpler solution. Yet I think there should be. I have two string[] arrays and I need to merge them into one 2D array.

Here is the code I got to:

    public static string[,] GetStructure(string FilePath)
    {
        try
        {
            xliff = XDocument.Load(Path.GetFullPath(FilePath));
            XNamespace ns = "http://sdl.com/FileTypes/SdlXliff/1.0";
            string[] ids = xliff.Descendants().Elements(ns + "tag-defs").Elements(ns + "tag").Elements(ns + "st").Select(e => e.Parent.Attribute("id").Value).ToArray();
            string[] elements = xliff.Descendants().Elements(ns + "tag-defs").Elements(ns + "tag").Elements(ns + "st").Select(e => e.Value).ToArray();

            string[,] mergedarray = new string[ids.Length, 2];

            for (int i = 0; i < ids.Length; i++)
            {
                mergedarray[i, 0] = ids[i];
                mergedarray[i, 1] = elements[i];
            }

            return mergedarray;
        }
        catch (Exception)
        {
            return null;
        }
    }

Any suggestion to make this merging simpler?

Upvotes: 0

Views: 75

Answers (1)

Sebastian Siemens
Sebastian Siemens

Reputation: 2421

You can use Linq.

var range = Enumerable.Range(0, ids.Length).ToList();
range.ForEach(i => { mergedarray[i, 0] = ids[i]; mergedarray[i, 1] = elements[i]; });

Maybe there is a better Linq statement

Upvotes: 1

Related Questions