Home > Enterprise >  Operator '/' cannot be applied to operands of type 'int' and 'Task'
Operator '/' cannot be applied to operands of type 'int' and 'Task'

Time:11-25

I need your help regarding an error im getting which i have mentioned on the subject,I have amethod which is :

           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;
                    }

                }
            }

i need to call it in another method ,i just write the part i get this error ,if you need i will share that as well bu im sure it wont help:

        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);

im using .NetCore 5,any help will be appreciated

CodePudding user response:

Think you probably wanted more like:

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; 
    }
}

And

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);

Note, you'll neeed to make this calling method async too.. But what return type you give it depends on what it returns. If it's void make it Task. If it's Something, make it Task<Something>

CodePudding user response:

If I understand correctly, the var midrate is not an int, so you can try to cast it to integer, which should be something like:

var amountInEurto = accumulatedAmount * (1 / Convert.ToInt32(midrate));
  • Related