C
C#2y ago
Maskoe

❔ Trying to generify List Func Task

Im trying to generify the following code.
string? contract = null;

var contractsPrio1 = await GetContractsPrio1(client);
contract = contractsPrio1.FirstOrDefault();

if (contract is null)
{
var contractsPrio2 = await GetContractsPrio2(client);
contract = contractsPrio2.FirstOrDefault();
}

if (contract is null)
{
var contractsPrio3 = await GetContractsPrio3(client);
contract = contractsPrio3.FirstOrDefault();
}
string? contract = null;

var contractsPrio1 = await GetContractsPrio1(client);
contract = contractsPrio1.FirstOrDefault();

if (contract is null)
{
var contractsPrio2 = await GetContractsPrio2(client);
contract = contractsPrio2.FirstOrDefault();
}

if (contract is null)
{
var contractsPrio3 = await GetContractsPrio3(client);
contract = contractsPrio3.FirstOrDefault();
}
I think my intention is clear, I have several ways to query contracts that are prioritized a certain way. If i dont find anything in the first priority list, check the 2nd, if i dont find anything, check the 3rd and so on. I got this so far
async Task<T?> SetToFirstElement<T>(List<Func<Task<List<T>>>> getElementsFunctions) where T : class
{
foreach (var getElementsFunction in getElementsFunctions)
{
var elements = await getElementsFunction.Invoke();
if (elements.Any())
return elements.First();
}

return null;
}
async Task<T?> SetToFirstElement<T>(List<Func<Task<List<T>>>> getElementsFunctions) where T : class
{
foreach (var getElementsFunction in getElementsFunctions)
{
var elements = await getElementsFunction.Invoke();
if (elements.Any())
return elements.First();
}

return null;
}
I can call it like this
var contract = await SetToFirstElement<string>(new()
{
() => GetContractsPrio1(client),
() => GetContractsPrio2(client),
() => GetContractsPrio3(client)
});
var contract = await SetToFirstElement<string>(new()
{
() => GetContractsPrio1(client),
() => GetContractsPrio2(client),
() => GetContractsPrio3(client)
});
Are there any issues with this? Does this work simpler? Working with tasks and lists of tasks and async await is always kinda dangerous. But it seems to work. The GetContractsPrioX methods fire only if needed. If I have an exception in one of them, everything seems to behave normally, nothing gets swallowed. I would like to remove the () => part, but I dont think its possible. If I remove it and just work with the GetContractsPrioX methods directly, the tasks naturally start executing instantly.
3 Replies
ero
ero2y ago
that can just be shortened to
string? contract =
await GetContractsPrio1(client).FirstOrDefault()
?? await GetContractsPrio2(client).FirstOrDefault()
?? await GetContractsPrio3(client).FirstOrDefault();
string? contract =
await GetContractsPrio1(client).FirstOrDefault()
?? await GetContractsPrio2(client).FirstOrDefault()
?? await GetContractsPrio3(client).FirstOrDefault();
Maskoe
MaskoeOP2y ago
oh wait, youre right jesus christ man, sometimes I get lost 😫
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server