Parallel problem.

I have a Parallel.Foreach loop that gives me sadness, and would like one of you guys to shed some light on this. Unfortunately, little can be found in googling.

Here is the node:

My foreach loop:

 string [] Ids = {........}; //a string array of ID's using(IUnitOfWork uw = GetUnitOfWork()) { Parallel.ForEach(Ids, currentRecord => { var x = (from h in uw.GetRepository<EFEntity1>().AsQueryable() join k in uw.GetRepository<EFEntity2>().AsQueryable() on h.ID equals k.ID join l in uw.GetRepository<EFEntity3>().AsQueryable() on h.FundAccount equals l.FundAccount where h.ID == currentRecord select new { hx, hy, hz}); foreach (var v in x) { if (v.SomeMember == "foo") { } Console.WriteLine("Output : {0} {1} {2} {3} {4} ", vx, vy, vz); } }); } 

The LINQ statement is where I get the ArgumentExcpetion message:

An item with the same key has already been added.

Any tips on what might be wrong with my implementation of the foreach loop in this scenario?

Appreciate the support.

thanks

+4
source share
1 answer

I solved this by moving the use brackets into the Parellel loop. The reason was that dbcontext is not thread safe.

+4
source

Source: https://habr.com/ru/post/1469066/


All Articles