Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain please AsParallel()

Can somebody explain me one thing. As I understand AsParallel() executes in own task. So, if query return huge amount of data the variable 'd' can be empty at time when 'foreach' started to execute Console.WriteLine?

var integerList = Enumerable.Range(1, 100);
var d = from x in integerList.AsParallel()
where x <= 25
select x;
foreach (var v in d)
{
Console.WriteLine(v);
}
like image 844
user628147 Avatar asked May 04 '11 15:05

user628147


1 Answers

AsParallel is a PLINQ feature. PLINQ automatically parallelizes local LINQ queries. PLINQ has the advantage of being easy to use in that it offloads the burden of both work partitioning and result collation to the Framework.

To use PLINQ, simply call AsParallel() on the input sequence and then continue the LINQ query as usual.

Variable d in your case can not be empty only because PLINQ. If it will be empty that means there is no elements in the collection that satisfy the condition x <= 25.

You can read more here

like image 140
oxilumin Avatar answered Oct 14 '22 10:10

oxilumin