I'm trying to implement parallelization in a project. The objective is to transform a classic ForEach that reads from a List<List<Foo>> and uploads to a database in a Parallale.ForEachAsync. My current implementation is:
var rows = new long();
foreach (var list in listOfLists)
{
var sb = new StringBuilder("replace into table values ");
foreach(item in list)
{
sp.Append($"({item.bar} ");
}
var updateRow = await _client.ExecuteSql(sb.ToString());
rows += updatedRows;
}
EF Core is not an option here. I need to pass the query as a string to the database, that's why I need to iterate a list of entities and build the query. Also, there is a limit on the query string size, that's why I have to break the original List<Foo> in smaller lists of List<List<Foo>>.
My current implementation is as follows:
var rows = new long();
await Parallel.ForEachAsync(listOfLists, async (list, token) =>
{
var sb = new StringBuilder("replace into table values ");
foreach(var item in list)
{
sp.Append($"({item.bar} ");
}
var updatedRows = await _client.ExecuteSql(sb.ToString());
rows += updatedRows;
}
But the problem is: when I run this code, It does not work as I expect, which is to do in parallalel, for each List<Foo> in List<List<Foo> the body function. When I ran the code, it flooded the database with duplicated entries.
How do I make a correct implementation of a async Parallel.ForEachAsync having as a <TSource> a List<List<Foo>>?
Edit: I'm using .net 6