I don't know enough about Parallel.ForEach to debug this, although I
do think you existing code would need a thead-safe .Add at least.
However, could you change this to:
ICollection<Customer> data = GetCustomers();
var customers = data.AsParallel().select(
(source) =>
{
DisplayableCustomer destination = new DisplayableCustomer();
Mapper.Map(source, destination);
return destination;
});
? (As a .NET 3.5 developer guessing....)
--
Regards,
Mark Hurd, B.Sc.(Ma.)(Hons.)
On 17 June 2013 22:15, David Rhys Jones <[email protected]> wrote:
> Oops sent too quickly.
>
> ICollection<Customer> data = GetCustomers();
> Parallel.ForEach(
> data,
> (source) =>
> {
> DisplayableCustomer destination = new DisplayableCustomer();
> Mapper.Map(source, destination);
> customers.Add(destination);
> });
>
> later on I order the data.
>
> var sorted = from c in customers orderby c.ZipCode descending select c;
>
> the error, occurs ( very rarely) in the orderby c.ZipCode
> NullReferenceException : c is null.
>
> What am I doing wrong? the parallel should all complete when it finishes
> right, it's not waiting for thread pools to be created / deleted and
> returning before the data is done?
>
> the ICollection is a List<Customer> but I could change that to a threadsafe
> list if needed.
>
> Davy,
>
> The US Congress voted Pizza sauce a vegetable. Don't even try to convince me
> of anything in the states is sane any more!
>