'rows with computesd values for time period in net core 6 app

I generating computed values based on a time range, like this: output

The trouble is that with many rows in takes for ever to do it.

I need to find a quicker way.

For each cell with volumes i do this:

public int GetVolume(int _month, int _year, int companyId, int NVR, string country)
{
      var salesCount = 0;

        salesCount = (int)(from sale in _context.SalesData.AsNoTracking()
                           join _company in _context.CompaniesMains.AsNoTracking() on sale.CompanyId equals _company.Id
                           where (sale.IYear == _year && sale.IMonth == _month) && _company.Id == companyId && sale.Nvr == NVR
                           && sale.Country.Trim().Equals(country)
                           select sale.SalesAmount).Sum();

        return salesCount;
    }

And in cells with turn over i do this:

   public decimal GetPrice(int _month, int _year, int NVR, string country, bool Euros = false)
    {
        var salesCount = 0m;

        var count = (from price in _context.Prices.AsNoTracking()
                     where price.IYear == _year
                     && price.IMonth == _month
                     && price.Valuta.Equals(country)
                     && price.Nvr.Equals(NVR)
                     select price.RegisterPrice.Trim()).FirstOrDefault();

        if (count != null)
        {
            var style = NumberStyles.AllowDecimalPoint;
            var culture = CultureInfo.CreateSpecificCulture("en-US");
            Decimal.TryParse(count, style, culture, out salesCount);
        }

        if (Euros)
        {
            var currency = 0m;
            switch (country)
            {
                case "DK":
                    currency = _memoryCache.GetCacheCurrency("dkk");
                    break;
                case "NO":
                    currency = _memoryCache.GetCacheCurrency("nok");
                    break;
                case "SE":
                    currency = _memoryCache.GetCacheCurrency("sek");
                    break;
                case "FI":
                    currency = _memoryCache.GetCacheCurrency("fin");
                    break;
            }

            salesCount /= currency;
        }

        return (decimal)salesCount;
    }

It all runs in a loop where i call the 2 methods.

 foreach (var q in selectedLst)
  {
     if (filters.TableType == TableTypeView.Month)
     {
        for (var i = 0; i <= GetMonthDifference; i++)
        {
                        var month = filters.StartDate.AddMonths(i);
                        var d = new Dates();
                        var salesAmount = GetVolume(month.Month, month.Year, q.CompanyId, q.Nvr, q.CompanyCountry);

                        if (filters.TurnOver)
                        {
                            var RegisterPrice = GetPrice(month.Month, month.Year, q.Nvr, q.CompanyCountry, filters.Euros);

                            d.TurnOver = (decimal)salesAmount * (RegisterPrice / 100);
                            d.RegisterPrice = RegisterPrice / 100;
                        }

                        d.Amount = (int)salesAmount;
                        d.Nvr = q.Nvr;
                        d.Month = month.Month;
                        d.Year = month.Year;
                        d.Country = q.CompanyCountry;
                        dates.Add(d);
          }
       }
}

Any ideas on how to make this quick?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source