I am using NET 6 Web Api.
I would like to process objects with the same identifier sequentially, whilst in parallel to different identifiers.
To illustrate the issue, given my object is as follows:
class Data{ public int Id {get;set;} public string o {get;set}}
So with a list as follows:
var l = new List<Data>(){
new Data() { Id = 1, o = "xxxxxx"},
new Data() { Id = 1, o = "yyyyyy"},
new Data() { Id = 12, o = "aaaaaaa"},
new Data() { Id = 12, o = "ccccccc"},
new Data() { Id = 97, o = "mmmmmmm"}
}
I would expect 3 “queues” in parallel. One processing Id=1
, another Id=12
and another Id=97
.
I have been using Channel<Data>
to foreach(var x in channel.Reader.ReadAllAsync(cancelationTken))
the data to process in a BackgroundService
. But this leads to one long queue, which then processes everything sequentically.
I am looking for pointers on how I might go about implementing such a requirement. Parallel.ForEach
came to mind – where it would group those with the same Id and put them in the same thread, but I am not sure if this grouping functionality is available yet.