天天看點

多條件組合查詢,解決方案記錄

1 今天的講解什麼?

多條件組合查詢,解決方案記錄

2 三種解決方案

  1. 多條件查詢技巧ado.net
  2. ef組合查詢
  3. dapper組合查詢(最喜歡用這個)

3 相關介紹

3.1 這個是什麼?

如何解決系統中常用的多條件組合查詢?解決方案記錄如下所示。

3.2 應用場景?

在開發系統中經常會這樣的需求,多個條件組合去查詢記錄。記得剛剛畢業那一會,寫這個功能感覺好難啊,後來在網上找到一些前輩們的教程,才解決這個功能,現在拿出來整理 一下(都是前輩們的成果),哈哈 ,本人菜雞,大牛勿噴。

4 下面來分别來看看三種解決方案的細節

4.1 多條件查詢技巧ado.net

#region 多條件搜尋時,使用List集合來拼接條件(拼接Sql)

            StringBuilder sql = new StringBuilder("select * from PhoneNum");
            List<string> wheres = new List<string>();
            if (cboGroup.SelectedIndex != 0)
            {
                wheres.Add(" ptypeid=" + cboGroup.Text.Split('|')[0]);
            }
            if (txtSearchName.Text.Trim().Length > 0)
            {
                 wheres.Add(" pname like '%" + txtSearchName.Text.Trim() + "%'");
            }
            if (txtSearchCellPhone.Text.Trim().Length > 0)
            {
                 wheres.Add(" pcellphone like '%" + txtSearchCellPhone.Text.Trim() + "%'");
            }

            //判斷使用者是否選擇了條件
            if (wheres.Count > 0)
            {
                string wh = string.Join(" and ", wheres.ToArray());
                sql.Append(" where " + wh);
            }
            #endregion
 
 
 
            #region 多條件搜尋使用帶參數的sql語句
            StringBuilder sql = new StringBuilder("select * from PhoneNum");
            List<string> wheres = new List<string>();
            List<SqlParameter> listParameter = new List<SqlParameter>();
            if (cboGroup.SelectedIndex != 0)
            {
                wheres.Add(" ptypeid=@typeid ");
                listParameter.Add(new SqlParameter("@typeid", cboGroup.Text.Split('|')[0]));
            }
            if (txtSearchName.Text.Trim().Length > 0)
            {
                wheres.Add(" pname like @pname ");
                //pname like '%喬%'
                //pname liek '%'+@pname+'%'
                listParameter.Add(new SqlParameter("@pname", "%" + txtSearchName.Text.Trim() + "%"));
            }
            if (txtSearchCellPhone.Text.Trim().Length > 0)
            {
                wheres.Add(" pcellphone like @cellphone ");
                listParameter.Add(new SqlParameter("@cellphone", "%" + txtSearchCellPhone.Text.Trim() + "%"));
            }

            //判斷使用者是否選擇了條件
            if (wheres.Count > 0)
            {
                string wh = string.Join(" and ", wheres.ToArray());
                sql.Append(" where " + wh);
            }
 
            SqlHelper.ExecuteDataTable(sql.ToString(), listParameter.ToArray());
            #endregion
           

原文連結

4.2 ef組合查詢

[HttpPost]
        public ActionResult Query(string city, string company,string contactName)
        {
            using (var context = new NorthwindEntities())
            {
                
                var parameter = Expression.Parameter(typeof(Customers));
                var type = typeof(Customers);
                Expression expr = Expression.Constant(true);
                var methodInfo = typeof(string).GetMethod("Contains", new Type[] { typeof(string) });
                if (!string.IsNullOrEmpty(city))
                {
                    expr = Expression.And(expr,
                        Expression.Equal(Expression.Property(parameter, "City"), Expression.Constant(city)));
                }
                if (!string.IsNullOrEmpty(company))
                {
                    expr = Expression.And(expr,
                        Expression.Call(Expression.Property(parameter, "CompanyName"), methodInfo, Expression.Constant(company)));
                }
                if (!string.IsNullOrEmpty(contactName))
                {
                    expr = Expression.And(expr,
                        Expression.Call(Expression.Property(parameter, "ContactName"), methodInfo, Expression.Constant(contactName)));
                }
                var lambda = Expression.Lambda<Func<Customers, bool>>(expr, parameter);
                return this.View(context.Customers.Where<Customers>(lambda).ToList<Customers>());
            }
        }
           

4.3 dapper組合查詢(最喜歡用這個)

4.3.1 關鍵點記錄如下
  1. 采用

    Dapper

    DynamicParameters

  2. 如果不了解

    Dapper

    ,可以部落格園中搜尋一下

    Dapper

    的相關方法
public List<MSys_Admin> GetAdminList(MSys_Admin model)
{
 string sqlText = "select count(1) from Sys_Admin where 1=1";
 var p = new DynamicParameters();
 if (!string.IsNullOrEmpty(model.LoginName))
 {
  sqlText += " and LoginName like @LoginName";
  p.Add("LoginName", model.LoginName+"%");
 }
 if (!string.IsNullOrEmpty(model.Name))
 {
  sqlText += " and Name like @Name";
  p.Add("Name","%"+ model.Name+"%");
 }
 using (var conn = Common.GetConn())
 {
  conn.Open();
  var r = conn.Query<MSys_Admin>(sqlText, p);
  conn.Close();
  return r.ToList();
 }
}
           

5 相關參考教程整理

  1. 多條件查詢--使用dapper指令參數動态拼接出最安全的sql語句
  2. 摘自本人的有道雲筆記
  3. ado.net相關
  4. ef相關
  5. dapper相關