我在测试运行报告时收到以下错误:
LINQ to Entities 无法识别方法 'System.Decimal PerformCurrencyConversion(System.Decimal, System.Decimal, System.Decimal, System.String, System.DateTime)' 方法,并且此方法无法转换为存储表达式。
这是我的查询:
List<BusinessPlanningElements> productSales = (from sale in ctn.ProductSales
where sale.DateSold.Year == reportRequest.Year && sale.ProductID == t.ProductID
group sale by sale.DateSold.Month into ds
select new BusinessPlanningElements
{
Month = ds.Select(it => it.DateSold.Month).FirstOrDefault(),
EURNumberOfUnitSold = ds.Where(it => it.EURSales.HasValue).Where(it => it.EURSales != 0).Count(),
GBPNumberOfUnitSold = ds.Where(it => it.GBPSales.HasValue).Where(it => it.GBPSales != 0).Count(),
USDNumberOfUnitSold = ds.Where(it => it.USDSales.HasValue).Where(it => it.USDSales != 0).Count(),
EURCumulativeNumberOfUnitsSold = ctn.ProductSales.Where(it => it.EURSales.HasValue && it.EURSales != 0 && it.ProductID == t.ProductID && it.DateSold.Month <= ds.Key).Count(),
GBPCumulativeNumberOfUnitsSold = ctn.ProductSales.Where(it => it.GBPSales.HasValue && it.GBPSales != 0 && it.ProductID == t.ProductID && it.DateSold.Month <= ds.Key).Count(),
USDCumulativeNumberOfUnitsSold = ctn.ProductSales.Where(it => it.USDSales.HasValue && it.USDSales != 0 && it.ProductID == t.ProductID && it.DateSold.Month <= ds.Key).Count(),
USDTotalSales = PerformCurrencyConversion(ds.Sum(it => it.USDSales.HasValue ? it.USDSales.Value : 0), 0M, 0M, reportRequest.Currency, DateTime.Now.AddMonths(-2)),
GBPTotalSales = PerformCurrencyConversion(0M, ds.Sum(it => it.GBPSales.HasValue ? it.GBPSales.Value : 0), 0M, reportRequest.Currency, DateTime.Now.AddMonths(-2)),
EURTotalSales = PerformCurrencyConversion(0M, 0M, ds.Sum(it => it.EURSales.HasValue ? it.EURSales.Value : 0), reportRequest.Currency, DateTime.Now.AddMonths(-2)),
USDCumulativeTotalSales = PerformCurrencyConversion(ctn.ProductSales.Sum(it => it.USDSales.HasValue && it.USDSales != 0 && it.ProductID == t.ProductID && it.DateSold.Month <= ds.Key ? it.USDSales.Value : 0), 0M, 0M, reportRequest.Currency, DateTime.Now.AddMonths(-2)),
GBPCumulativeTotalSales = PerformCurrencyConversion(ctn.ProductSales.Sum(it => it.GBPSales.HasValue && it.GBPSales != 0 && it.ProductID == t.ProductID && it.DateSold.Month <= ds.Key ? it.GBPSales.Value : 0), 0M, 0M, reportRequest.Currency, DateTime.Now.AddMonths(-2)),
EURCumulativeTotalSales = PerformCurrencyConversion(ctn.ProductSales.Sum(it => it.EURSales.HasValue && it.EURSales != 0 && it.ProductID == t.ProductID && it.DateSold.Month <= ds.Key ? it.EURSales.Value : 0), 0M, 0M, reportRequest.Currency, DateTime.Now.AddMonths(-2)),
}).ToList();
PerformCurrencyConversion 方法几乎可以获取转换率列表,并将值转换为用户指定的货币。它接受如下参数:
public decimal PerformCurrencyConversion(decimal usd, decimal gbp, decimal eur, string Currency, DateTime saleMonth)
{
//Logic
}
过去我在整个代码库中多次使用过这种方法,没有任何问题或错误,例如
List<MonthlyProductSales> monthlyProductSales = (from sale in productSales
orderby sale.DateSold descending
group sale by new { sale.DateSold.Month, sale.Product.Name } into ds
select new MonthlyProductSales
{
Name = ds.Select(it => it.Product.Name).FirstOrDefault(),
Month = ds.Select(it => it.DateSold.Month).FirstOrDefault(),
Year = ds.Select(it => it.DateSold.Year).FirstOrDefault(),
USDNumberItemsSold = ds.Where(it => it.USDSales.HasValue).Where(it => it.USDSales != 0).Count(),
GBPNumberItemsSold = ds.Where(it => it.GBPSales.HasValue).Where(it => it.GBPSales != 0).Count(),
EURNumberItemsSold = ds.Where(it => it.EURSales.HasValue).Where(it => it.EURSales != 0).Count(),
USDRevenueTotal = PerformCurrencyConversion(ds.Sum(it => it.USDSales.HasValue ? it.USDSales.Value : 0), 0M, 0M, reportRequest.Currency, DateTime.Now.AddMonths(-2)),
GBPRevenueTotal = PerformCurrencyConversion(0M, ds.Sum(it => it.GBPSales.HasValue ? it.GBPSales.Value : 0), 0M, reportRequest.Currency, DateTime.Now.AddMonths(-2)),
EURRevenueTotal = PerformCurrencyConversion(0M, 0M, ds.Sum(it => it.EURSales.HasValue ? it.EURSales.Value : 0), reportRequest.Currency, DateTime.Now.AddMonths(-2)),
}).ToList();
谁能看到有什么区别,或者我如何解决这个错误?