I have a method that accepts an IEnumerable
and returns it transformed using the yield operator. To transform one element of the enumerable, I need to first know the value of another element. Therefore, I thought of using a TaskCompletionSource
to create something like a "promise".
The problem here is that this code results in a deadlock if anything other than "a" is the value of the first TestFieldA
. One solution would be to order the enumerable before passing it into the method - in which case there is no need for TaskCompletionSource
altogether. I would like to know however if it can be done without this. I also know that this can be done with some LINQ queries, but this would require enumerating the input several times, which I would like to avoid.
This is is what I'm trying to achieve. (Only works if the first TestFieldA == "a"
)
class Test
{
public string TestFieldA {get;set;}
public int TestFieldB {get;set;}
}
private async IAsyncEnumerable<Test> Transform(IEnumerable<Test> inputEnumerable)
{
var tcs = new TaskCompletionSource<int>();
foreach(var input in inputEnumerable)
{
if (input.TestFieldA == "a")
{
tcs.SetResult(input.TestFieldB);
yield return input;
}
else
{
input.TestFieldB -= await tcs.Task;
yield return input;
}
}
}