首页 > 解决方案 > 如何使用mysql数据库在ASP.Net core 2.0 MVC中按多个过滤字段进行搜索和排序

问题描述

如何基于 ASP.Net 核心 MVC 中的多个过滤器字段进行搜索。

我想在 MySQL 数据库中搜索数据,然后在 windows Visual Studio 2017 中使用多个过滤器过滤搜索到的数据。

模型类

namespace i.model
{
public class CompanyRecords
{
    public int? id { get; set; }
    public int? user_id { get; set; }
    public string product_name { get; set; }
    public string Keywords { get; set; }
    public string category { get; set; }
    public string description { get; set; }
    public string modules { get; set; }
}
[enter image description here][1]}

上下文模型

 public List<CompanyRecords> GetProducts(string data,string idd, SearchModel searchSch)
    {   using (MySqlConnection conn = GetConnection())
        {
            conn.Open();
            List<CompanyRecords> results = conn.Query<CompanyRecords>("SELECT * FROM product WHERE category LIKE '%" + data + "%' OR Keywords LIKE '%" + data + "%' OR product_name LIKE '%" + data + "%' OR description LIKE '%" + data + "%' OR modules LIKE '%" + data + "%'OR metatags LIKE '%" + data + "%'OR geographical_focus LIKE '%" + data + "%'").ToList();

            //if (idd == "Category")
              if (!string.IsNullOrEmpty(searchSch.category))
            {
                //List<CompanyRecords> result = conn.Query<CompanyRecords>("SELECT * FROM product WHERE category LIKE '%" + data + "%'").ToList();


               List<CompanyRecords> result = conn.Query<CompanyRecords>(results.Where(x=>x.category.Contains(searchSch.category))).ToList();

                return result;
            }
            else if (idd == "Keywords")
            {
                List<CompanyRecords> result = conn.Query<CompanyRecords>("SELECT * FROM product WHERE Keywords LIKE '%" + data + "%'").ToList();
                return result;
            }
            else if (idd == "Modules") {
                List<CompanyRecords> result = conn.Query<CompanyRecords>("SELECT * FROM product WHERE modules LIKE '%" + data + "%'").ToList();
                return result;
            }
            return results;         
            //List<CompanyRecords> results = conn.Query<CompanyRecords>("SELECT * FROM product WHERE category LIKE '%" + data + "%'OR description LIKE '%" + data + "%' OR modules LIKE '%" + data + "%'OR metatags LIKE '%" + data + "%'OR geographical_focus LIKE '%" + data + "%'OR target_job_titles LIKE '%" + data + "%'OR target_industry_type LIKE '%" + data + "%'OR desktop_web_both LIKE '%" + data + "%'OR dept_user_type LIKE '%" + data + "%'OR semantic LIKE '%" + data + "%'OR cognitive LIKE '%" + data + "%'OR target_campany_size LIKE '%" + data + "%'").ToList();
            //if (results != null)
            //{ return results; }
            //return results;
        }  
    }

听到 conn.Open(); List results = conn.Query("SELECT * FROM product WHERE category LIKE '%" + data + "%' OR Keywords LIKE '......).ToList(); 给出正确的结果现在我想过滤搜索到的数据是具有多个过滤器(如类别、模块关键字等)的结果...

标签: asp.netasp.net-mvcasp.net-mvc-4asp.net-coreasp.net-core-2.0

解决方案


推荐阅读