For the life of me, I can't figure out why all the foos are not null.
I'm assuming the .ForAll()
should be executing before I call the .All()
method, but it's not?
public class Foo
{
public string Bar { get; set; }
}
static void Main(string[] args)
{
var foos = new List<Foo> { new Foo(), new Foo(), new Foo() };
var newFoos = foos
.AsParallel()
.Select(x =>
{
x.Bar = "";
return x;
});
newFoos.ForAll(x => x = null);
var allFoosAreNull = newFoos.All(x => x == null);
Console.WriteLine(allFoosAreNull); // False ??
}
LINQ to Objects and LINQ to XML queries are designed to work sequentially, and do not involve multi-threading, concurrency, or parallel computing.
AsParallel extension method on the source sequence and executing the query by using the ParallelEnumerable. ForAll method. This documentation uses lambda expressions to define delegates in PLINQ. If you are not familiar with lambda expressions in C# or Visual Basic, see Lambda Expressions in PLINQ and TPL.
AsParallel(IEnumerable)Enables parallelization of a query. public: [System::Runtime::CompilerServices::Extension] static System::Linq::ParallelQuery ^ AsParallel(System::Collections::IEnumerable ^ source); C# Copy.
Parallel LINQ (PLINQ) is a parallel implementation of the Language-Integrated Query (LINQ) pattern. PLINQ implements the full set of LINQ standard query operators as extension methods for the System. Linq namespace and has additional operators for parallel operations.
When you do this
newFoos.ForAll(x => x = null);
you are assigning null
to x
, which is the parameter of your lambda. x
is local to the lambda. It is not a ref
parameter, and assigning values to it has no effect outside of its body. Effectively, that line does nothing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With