Question

I am attempting to take two string collections and combine each line in both collections simultaneously to show a full file path for my user.

Examples to help clear up confusion:

  • String Collection 1 will contain a list of paths.
    Example:

    C:\windows\xxxx\xxx\xx, C:\Users\xxx\xxx, C:\test\xxx\xxx
    
  • String Collection 2 will contain a list of file names.
    Example:

    file.txt, asd.txt, mydll.dll
    

Each list holds the key to one another in the same line number and I just need to combine them to output them to the end user.

String Collection Line 1 + SC2 Line 1 = Path
SC Line 2 + SC2 Line 2 = Path
SC Line 3 + SC2 Line 3 = Path

The information is not accessible in a combined state so I will be placing the data into two separate WPF TextBoxes and then their content will be pulled into a stringcollection.

First Collection

StringCollection lines = new StringCollection();
int lineCount = filePath.LineCount;

for (int line = 0; line < lineCount; line++)
    // Get line text and add to string collection
    lines.Add(filePath.GetLineText(line));

Second Collection

StringCollection lines2 = new StringCollection();
int lineCount2 = fileName.LineCount;

for (int line = 0; line < lineCount; line++)
    // Get line text and add to string collection
    lines.Add(fileName.GetLineText(line));

Any and all help is appreciated!

Edit 1

I have experimented with the ZIP command thanks to Eve but I also found an alternate route. Is there a safer route using zip compared to the code below?

Keep in mind I will have a function to check the line counts and make sure they are the same.

        StringCollection lines = new StringCollection();
        int lineCount = itemIDBox.LineCount;

        for (int line = 0; line < lineCount; line++)
        {
            string id;
            string rev;
            string combined;

            id = itemIDBox.GetLineText(line);
            rev = revBox.GetLineText(line);

            combined = id + @"\" + rev;

            lines.Add(combined);
        }
Was it helpful?

Solution

You can use the Zip method from System.Linq.

var fullPaths = lines.Cast<string>().
    Zip(lines2.Cast<string>(), (path, fileName) => Path.Combine(path, fileName)).
    ToArray();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top