Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select from linq statement with await

This seems like it should be very simple, I have the following code

var additionalInformation=   response.AdditionalInformation.Select( async x =>  new AdditionalInformationItem
                {
                    StatementCode = x?.StatementCode?.Value,
                    LimitDateTime = x?.LimitDateTime?.Item?.Value,
                    StatementTypeCode = x?.StatementTypeCode?.Value,
                    StatementDescription = x?.StatementDescription?.Value,
                    AdditionalInformationResult = await BuildAdditionalInformationPointers(x)


                }).ToList();

What I am trying to achieve is for additionalInformation to be of type

List<AdditionalInformationItem>

, what I am getting is List<Task<AdditionalInformationItem>>

Can anyone help me correctly reformulate my statement?

like image 766
jazza1000 Avatar asked Dec 30 '25 14:12

jazza1000


2 Answers

You need to unwrap the tasks using, await Task.WhenAll(additionalInformation) then you access the actual result using additionalInformation[0].Result.

So something like this:

var additionalInformation=   response.AdditionalInformation.Select( async x =>  new AdditionalInformationItem
                {
                    StatementCode = x?.StatementCode?.Value,
                    LimitDateTime = x?.LimitDateTime?.Item?.Value,
                    StatementTypeCode = x?.StatementTypeCode?.Value,
                    StatementDescription = x?.StatementDescription?.Value,
                    AdditionalInformationResult = await BuildAdditionalInformationPointers(x)


                });

await Task.WhenAll(additionalInformation);
//This will iterate the results so may not be the most efficient method if you have a lot of results
List<AdditionalInformationItem> unwrapped = additionalInformation.Select(s => s.Result).ToList();
like image 122
Liam Avatar answered Jan 01 '26 03:01

Liam


There is actually no way to do this in a simple lambda expression. The async lambda expression will always return a Task<T>. So you can go quick and dirty and call .Result on the Task (don't do it! Except if you await first as in Liam's answer), or simply initialize a new list and add items in a foreach loop:

var additionalInformation = new List<AdditionalInformationItem>();
foreach (var x in response.AdditionalInformation)
{
    var item = new AdditionalInformationItem
        {
            StatementCode = x?.StatementCode?.Value,
            LimitDateTime = x?.LimitDateTime?.Item?.Value,
            StatementTypeCode = x?.StatementTypeCode?.Value,
            StatementDescription = x?.StatementDescription?.Value,
            AdditionalInformationResult = await BuildAdditionalInformationPointers(x)
        };

    additionalInformation.Add(item);
}
like image 37
Jesse de Wit Avatar answered Jan 01 '26 02:01

Jesse de Wit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!