user3818229
user3818229

Reputation: 1637

Collection.AsParallel() + yield return

My qestion is about the result of combination PLINQ and yield keyword. What will happen if i write like so:

//Some stuff here
foreach (var x in collection.AsParallel())
{
    yield return new CustomObject
    {
        property1 = //Large calculations here
        property2 = x.Name
        //... etc.
    };
}

What do I really want? I want to initialize objects in different threads but return them when some of them needs.
Thanks in advance!

Upvotes: 0

Views: 589

Answers (1)

Servy
Servy

Reputation: 203840

AsParallel just enables the use of parallel versions of the LINQ operations. It doesn't actually do anything in and of itself.

So your code, outside of a tiny bit of extra overhead, is functionally no different from you having omitted AsParallel entirely.

If you want to actually be able to perform the construction of the CustomObject instances in parallel, and you want to use PLINQ to do it (there are of course any number of other tools you can use to create objects in parallel), then you'll want to use Select to transform a sequence of one type into a sequence of another type, rather than using your own custom iterator block.

Upvotes: 3

Related Questions