开发者

How do I pass 2 lists into Parallel.ForEach?

How do I pass 2 lists into Parallel.ForEach?

Example:

List<Person> a = new List<Person>() { new Person(), new 开发者_StackOverflow社区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
});  

I would prefer to avoid encapsulating Person and Car into Object container. Is this possible?


If you're using .NET 4 (which you probably are) and you're trying to pair the first Person with the first Car etc, you can just use 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;
});


You are looking for Enumerable.Zip

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜