我需要你的幫助來解決我在這個問題上提到的錯誤,我有一個方法:
public Task GetMidRateInEuro(string currencypair)
{
using (var db = this.dbContextFactory.Create())
{
try
{
//result is decimal
var midrate = db.Fxrates.Where(s => s.CurrencyPair == currencypair).Select(r => r.MidRate).FirstOrDefault();
if (midrate != null)
return Task.FromResult(midrate);
}
else
{
return Task.FromResult(false);
}
}
catch (Exception ex)
{
this.logger.Log(currencypair "midrate" ex.Message, true);
throw ex;
}
}
}
我需要用另一種方法呼叫它,我只是寫了我得到這個錯誤的部分,如果你需要我也會分享它,但我確定它不會有幫助:
var midrate = GetMidRateInEuro("EUR" currency);
//here is where i get error Operator '/' cannot be applied to operands of type 'int' and 'Task'
var amountInEurto = accumulatedAmount * (1 / midrate);
我正在使用 .NetCore 5,任何幫助將不勝感激
uj5u.com熱心網友回復:
認為你可能想要更多:
public async Task<decimal> GetMidRateInEuroAsync(string currencypair)
{
using var db = this.dbContextFactory.Create();
try
{
//result is decimal?
var midrate = (await db.Fxrates.FirstOrDefaultAsync(s => s.CurrencyPair == currencypair))?.MidRate;
if (midrate.HasValue)
return midrate.Value;
//or whatever kind of not-exists handling you want
throw CurrencyPairDoesNotExistException(currencypair);
}
catch (Exception ex)
{
this.logger.Log(currencypair "midrate" ex.Message, true);
throw;
}
}
和
var midrate = await GetMidRateInEuroAsync("EUR" currency);
//here is where i get error Operator '/' cannot be applied to operands of type 'int' and 'Task'
var amountInEurto = accumulatedAmount * (1 / midrate);
請注意,您也需要創建此呼叫方法async。但是您給它的回傳型別取決于它回傳的內容。如果它void做到了Task。如果它是東西,讓它Task<Something>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/366915.html
