在 ASP.NET Core 中使用 Linq 进行动态过滤

Dynamic filtering using Linq in ASP.NET Core

我是 Linq 查询的新手。如果附加图像中有 1 个或多个字段 is/are null:

,我只想使我的过滤动态化

由于我习惯了硬编码查询,如果我只选择上面的这3项,我无法推导出代码。如何让下面这段代码变成动态的?

var query = db1.sub_project.Where
            (x =>
                (
                
                (x.sub_project_id == Geotagging_SP_ID && x.sub_project_id != null) &&
                (x.sub_project_name == sub_project_name && x.sub_project_name != null) &&
                (x.cycle_id == Cycle && x.cycle_id != null) &&
                (x.prov_code == Province && x.prov_code != null) &&
                (x.region_code == Region && x.lib_regions.region_code != null) &&
                (x.city_code == Municipality && x.city_code != null) &&
                (x.brgy_code == Barangay && x.brgy_code != null) &&
                (x.lib_project_type.major_classification_id == major_classification_id && x.lib_project_type.major_classification_id != null) &&
                ((x.final_physical_accomplishment >= final_physical_accomplishment_From && x.final_physical_accomplishment != null) && (x.final_physical_accomplishment <= final_physical_accomplishment_To && x.final_physical_accomplishment != null)) &&
                (x.mode_id == mode_ids && x.mode_id != null) &&
                (x.project_type_id == Project_Type && x.project_type_id != null)

                )
                

            ).Select(x => new 
            {
                PkID = x.ID,
                Geotagging_SP_ID = x.sub_project_id, 
                sub_project_name = x.sub_project_name, 
                Project_Type = x.lib_project_type.Name,
                Project = x.lib_modality.name,
                last_updated_date = x.last_updated_date, 
                created_date = x.created_date, 
                sql_Tuning_Final_physical = x.lib_physical_status.Name,
                sql_Tuning_Final_Physical_Accomplishment = x.final_physical_accomplishment,
                Cycle = x.lib_cycle.cycle_name, 
                Region = x.lib_regions.region_name, 
                Province = x.lib_provinces.prov_name, 
                Municipality = x.lib_cities.city_name,
                Barangay = x.lib_brgy.brgy_name
            }).ToList();

            result = query; 

谢谢。

要执行过滤,请将所有输入参数定义为可为空,例如:

public class FilteringParameters
{
   public int? Geotagging_SP_ID { get; set; }
   public string sub_project_name { get; set; }

   //other properties
 }

然后检查您的方法中过滤器的所有参数,如下所示:如果反对是 Null 或 Empty,则根据它过滤您的查询。最后,调用 ToList 或 ToListAsync 方法。

public async Task<List<ProjectViewModel>> GetProjectsAsync(FilteringParameters parameters)
{
    var query = db1.sub_project.AsQueryable();

      if (parameters.Geotagging_SP_ID != null)
      {
         query = query.Where(x => x.sub_project_id == parameters.Geotagging_SP_ID);
      }

      if (!string.IsNullOrWhiteSpace(parameters.sub_project_name))
      {
         query = query.Where(x =>x.sub_project_name == parameters.sub_project_name);
      }

     if(parameters.Cycle != null)
      {
         query = query.Where(x => x.cycle_id == parameters.Cycle);
      }


      //etc
      //check other filtering parameters
      //and

   var result = await query.Select(x => new ProjectViewModel
        {
            PkID = x.ID,
            Geotagging_SP_ID = x.sub_project_id,
            sub_project_name = x.sub_project_name,
            Project_Type = x.lib_project_type.Name,
            Project = x.lib_modality.name,
            last_updated_date = x.last_updated_date,
            created_date = x.created_date,
            sql_Tuning_Final_physical = x.lib_physical_status.Name,
            sql_Tuning_Final_Physical_Accomplishment = x.final_physical_accomplishment,
            Cycle = x.lib_cycle.cycle_name,
            Region = x.lib_regions.region_name,
            Province = x.lib_provinces.prov_name,
            Municipality = x.lib_cities.city_name,
            Barangay = x.lib_brgy.brgy_name
        }).ToListAsync();

   return result;
}