I need a bit of help. I have a list of 5k elements that is a path to files. I split the list into five smaller lists. My problem is how can I loop over the list of lists and pick the elements from all five at the same iteration.
Sample of code. The source list:
List<string> sourceDir = new List<string>(new string[] { "C:/Temp/data/a.txt", "C:/Temp/data/s.txt", "C:/Temp/data/d.txt", "C:/Temp/data/f.txt", "C:/Temp/data/g.txt", "C:/Temp/data/h.txt", "C:/Temp/data/j.txt", "C:/Temp/data/k.txt", "C:/Temp/data/l.txt", "C:/Temp/data/z.txt"});
Splitting the list into smaller list:
public static List<List<T>> Split<T>(IList<T> source)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / 2)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
Result:
var list = Split(sourceDir);
As result in the variable list, I get five lists. How can I now access items from all lists at one iteration for further processing? Something like:
foreach (string fileName in list[0])
{
foreach (string fileName1 in list[1])
{
foreach (string fileName2 in list[2])
{
foreach (string fileName3 in list[3])
{
foreach (string fileName4 in list[4])
{
//have items from all lists
Console.WriteLine("First name is: " + fileName);
Console.WriteLine("Second name is: " + fileName1);
Console.WriteLine("Third name is: " + fileName2);
Console.WriteLine("Fourth name is: " + fileName3);
Console.WriteLine("Fift name is: " + fileName4);
break;
}
break;
}
break;
}
break;
}
continue;
}
the above foreach loop is just to get an idea of what I need.