简体   繁体   English

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

[英]Dynamic filtering using Linq in ASP.NET Core

I am new in Linq query.我是新来的Linq查询。 I just want to make my filtering dynamic if 1 of the fields or more in image attached is/are null:如果附加图像中的 1 个或多个字段是 null,我只想使我的过滤动态化:

在此处输入图像描述

Since I am used to hardcode query, I can't derive the code if I only choose of those 3 items above.由于我习惯于硬编码查询,如果我只选择上面的这 3 项,我将无法导出代码。 How to make this code below become dynamic?如何使下面的代码成为动态的?

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; 

Thank you.谢谢你。

To perform the filtering, define all your input parameters as nullable, for example:要执行过滤,请将所有输入参数定义为可为空,例如:

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

   //other properties
 }

Then check all the parameters of your filter in your method as follows: If the opposition is Null or Empty, filter your query based on it.然后检查您的方法中过滤器的所有参数,如下所示:如果反对是 Null 或 Empty,则根据它过滤您的查询。 Finally, call the ToList or ToListAsync method.最后,调用 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM