LINQ 表达式可以翻译

问题描述 投票:0回答:1

我想在 EF core 中应用一些高级过滤,但出现错误; 这是我的代码:

   private IQueryable<AppDataUsage> PrepareDataUsageFilter(IQueryable<AppDataUsage> dataUsageReports, DataUsageFilter dataUsageFilter, List<AppDataUsageSettings> reportSettings, AppDataUsageSettings defaultSetting, int minData, int MaxData)
   {
       if (dataUsageFilter == DataUsageFilter.Custom)
       {
           return dataUsageReports.Where(a => a.TotalMobileDataUsage >= minData && a.TotalMobileDataUsage <= MaxData);
       }
       else if (dataUsageFilter == DataUsageFilter.Exceeded)
       {
           return dataUsageReports.Where(a => a.TotalMobileDataUsage > GetDataLimit(a, reportSettings, defaultSetting));
       }
       else if (dataUsageFilter == DataUsageFilter.Approaching)
       {
           return dataUsageReports.Where(a => a.TotalMobileDataUsage >= GetDataLimit(a, reportSettings, defaultSetting) &&
               a.TotalMobileDataUsage < GetDataWarning(a, reportSettings, defaultSetting));
       }

       return dataUsageReports;
   }

   
   private static byte GetDataLimit(AppDataUsage dataUsage, List<AppDataUsageSettings> reportSettings, AppDataUsageSettings defaultSetting)
   {
       var policyId = dataUsage.PolicyId;
       var groupId = dataUsage.GroupId;

       var dataLimit = reportSettings
           .Where(r => r.SettingMappings.Any(x => x.ObjectId == policyId || x.ObjectId == groupId))
           .Select(r => r.DataLimit)
           .DefaultIfEmpty(defaultSetting.DataLimit)
           .FirstOrDefault();

       return dataLimit;
   }

   private static int GetDataWarning(AppDataUsage dataUsage, List<AppDataUsageSettings> reportSettings, AppDataUsageSettings defaultSetting)
   {
       var policyId = dataUsage.PolicyId;
       var groupId = dataUsage.GroupId;

       var dataWarning = reportSettings
           .Where(r => r.SettingMappings.Any(x => x.ObjectId == policyId || x.ObjectId == groupId))
           .Select(r => r.DataWarning)
           .DefaultIfEmpty(defaultSetting.DataWarning)
           .FirstOrDefault();

       return dataWarning;
   }

错误显示“LINQ 表达式可以被翻译”。我认为这是因为我试图获取“DataLimit”和“DataWarning”的方式,但不确定如何解决这个问题。创建单独函数的原因是因为我需要在单个方法中的多个位置执行相同的操作

c# .net entity-framework .net-core entity-framework-core
1个回答
0
投票

尝试从表达式中提取方法调用:

..
else if (dataUsageFilter == DataUsageFilter.Approaching)
{
    int dataWarning = GetDataWarning(a, reportSettings, defaultSetting);
    int dataLimit = GetDataLimit(a, reportSettings, defaultSetting);

    return dataUsageReports.Where(a => a.TotalMobileDataUsage >= dataLimit  && a.TotalMobileDataUsage < dataWarning);
}
© www.soinside.com 2019 - 2024. All rights reserved.