如何将2个列表传递到Parallel.ForEach中?

例子:

List<Person> a = new List<Person>() { new Person(), new Person(), new Person() };
List<Car> b = new List<Car>() { new Car(), new Car(), new Car() };

//PSEUDO CODE
Parallel.ForEach(a, b, (person, car) => {
    //WORK ON person, WORK ON car
});

我宁愿避免将Person和Car封装到Object容器中。这可能吗?

最佳答案

如果您使用的是.NET 4(可能是),并且尝试将第一个Person与第一个Car等配对,则可以只使用 Zip :

List<Person> a = new List<Person>() { new Person(), new Person(), new Person() };
List<Car> b = new List<Car>() {} { new Car(), new Car(), new Car() };
var zipped = a.Zip(b, (person, car) => new { person, car });

Parallel.ForEach(zipped, pair => {
    Person person = pair.person;
    Car car = pair.car;
});

关于c# - 如何将2个列表传递给Parallel.ForEach?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5668285/

10-12 00:36
查看更多