asp.net - 如何使用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(); 给出正确的结果现在我想过滤搜索到的数据是具有多个过滤器(如类别、模块关键字等)的结果...
解决方案
推荐阅读
- apache - Vue CLI。使用子域发布
- python - Python中的数据拟合多个峰
- android - 如何在 Android XML 布局中获取状态栏高度?
- reactjs - 无法从 getInitialProps 获取道具
- python - 为什么对激活值(Softmax)的预测会给出不正确的结果?
- python - 为什么按空格键时屏幕上没有项目符号
- tensorflow - 如何根据模型输入裁剪 keras 图像张量?
- node-modules - 使用“npm install”时如何自动将模块规则添加到 react-scripts 的 webpack.config.js
- javascript - node.js - Express:向 res.render() 发送参数
- swift - 为什么我在 Xcode 11 Beta 5 中使用 ForEach 时会出错?