Zipping a list in thread safe manner

At the moment, I am using the following pseudo-code:

List<MyMail> m = new List<MyMail>();
foreach(var user in users) {
    var mails = mailLibrary.getAllMails(user); // synchronously; 100ms waiting time
    foreach(var mail in mails) m.add(mail);
}
return m.ToArray();

Now I want to fetch mail of all users in parallel, since the mailLibrary is written synchronously, and multiple mail servers are involved. Still, the list should be sorted the same way it was before. So I would try

List<MyMail> m = new List<MyMail>();
Parallel.ForEach(users,user => {
    List<MyMail> innerM = new List<MyMail>();
    var mails = mailLibrary.getAllMails(user); // synchronously; 100ms waiting time
    foreach(var mail in mails) innerM.add(mail);
    // How to zip innerM list content into m such that content is at the correct position?
});
return m.ToArray();

How can I zip innerM into m at the correct position?

Jon Skeet
people
quotationmark

Have you considered using Parallel LINQ instead?

return users.AsParallel().AsOrdered()
            .SelectMany(user => mailLibrary.getAllMails(user))
            .ToArray();

I believe that will do the right thing, in terms of maintaining all the ordering, according to the documentation.

people

See more on this question at Stackoverflow