I tried to use await in my lambda function to have an operation done asynchronously, to simplify the question I left out unnecessary operations (where clauses etc) and I named the async method Services.CustomerAmountService.QueryDbAndReturnResult
The function takes a double (sum of amount) and does numerous tasks, but most importantly an async call (firstAsync, which makes the function awaitable)
[HttpGet]
public async Task<IActionResult> GetCustomersAndAmount()
{
try
{
return Ok((await _orderRepository.CustomMadeQueryThatReturnsIQueryable()
.Select(x => new
{
CustomerId = x.CustomerId,
CustomerName = x.Customer.Name
Amount = x.Amount
})
.ToListAsync())
.GroupBy(x => x.CustomerId )
// this "async x" causes the unwanted result in my Ok() IActionResult
.Select( async x => new
{
CustomerName = x.First().CustomerName,
SumAmount = await Services.CustomerAmountService.QueryDbAndReturnResult(x.Sum(x => x.Amount))
})
));
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
The result looks like this:
{"1":[{"result":{"customerName":"VeryNiceCustomer","amount":1.0},"id":1269,"exception":null,"status":5,"isCanceled":false,"isCompleted":true,"isCompletedSuccessfully":true,"creationOptions":0,"asyncState":null,"isFaulted":false}]}
But the desired result and the result without "async x" and an async function (so just returning '1' and doing no further operations at all) looks like this:
{"1":[{"customerName":"VeryNiceCustomer","amount":1}]}
Im wondering what I can change to always get the second result, and not worry about returning unnecessary data about the operation ? It seems to be returing a Result, but I'm only interested in the Result of the Result (what the function returns, not the async Result)