溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

LINQ模糊查詢怎么使用

發(fā)布時間:2021-12-02 09:33:54 來源:億速云 閱讀:246 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“LINQ模糊查詢怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“LINQ模糊查詢怎么使用”吧!

LINQ模糊查詢實(shí)現(xiàn)的多條件復(fù)合搜索效果如下圖:

LINQ模糊查詢怎么使用

LINQ模糊查詢實(shí)現(xiàn)階段一:

利用Lambda表達(dá)式樹可以進(jìn)行動態(tài)查詢。寫了個方法進(jìn)行復(fù)合查詢,動態(tài)組合條件,生成Lambda表達(dá)式。

/// <summary>  /// 這個方法帶分頁功能,通過輸入的鍵值對NVC進(jìn)行復(fù)合查詢  /// </summary>  List<UserT_TractInfo> GetPagedObjectsByNVC(  int startIndex, int pageSize,   NameValueCollection nvc, bool isAnd)  {  IQueryable<UserT_TractInfo> query =   Consulting.Instance.UserT_TractInfo;  query.Where(t => t.IsDel == 0).  Where(t => t.IsAuditing == 1);//審核和邏輯刪除   Expression condition = null;  ParameterExpression param = Expression.  Parameter(typeof(UserT_TractInfo), "c");  int propertyCount = 0;  foreach (string key in nvc)  {  Expression right = Expression.Constant(nvc[key]);//鍵  string keyProperty = key;//屬性  if (typeof(UserT_TractInfo).GetProperty(keyProperty) != null)  //當(dāng)對象存在此屬性時(因?yàn)殒I值對可能還有很多其他的參數(shù),例如page)  {    Expression left = Expression.Property(param,  typeof(UserT_TractInfo).GetProperty(keyProperty));//建立屬性    Expression filter = Expression.Equal(left, right);//過濾器    if (condition == null)    {  condition = filter;    }    else   {   if (isAnd)   {  condition = Expression.And(condition, filter);   }   else  {  condition = Expression.Or(condition, filter);   }    }    propertyCount++;  }  }  //以上foreach組合了各個有效的鍵值對對應(yīng)的conditionExpression,  //復(fù)合查詢最重要的組合工作就這么完了  if (propertyCount > 0)  {  Expression pred = Expression.Lambda(condition, param);  MethodCallExpression whereCallExpression =   Expression.Call(typeof(Queryable), "Where",   new Type[] { typeof(UserT_TractInfo) },   Expression.Constant(query), pred);   return Consulting.Instance.UserT_TractInfo.AsQueryable().  Provider.CreateQuery<UserT_TractInfo>(whereCallExpression).  OrderByDescending(t => t.ID).Skip(startIndex - 1).  Take(pageSize).ToList();//查詢出結(jié)果  }  else {  return Consulting.Instance.UserT_TractInfo.  OrderByDescending(t => t.ID).Skip(startIndex - 1).  Take(pageSize).ToList();  //如果沒有有效鍵值對,則返回全部結(jié)果  }  }

搞了半天本來很興奮的,之后才知道Lambda表達(dá)式是寫不出.Contains()的,我的心瓦涼瓦涼的。

LINQ模糊查詢實(shí)現(xiàn)階段二:

雖然李永京的文章沒給我多少幫助,但它后面有個回復(fù)很有價值:“用微軟提供的System.Linq.Dynamic方便點(diǎn)。”很快找到了對應(yīng)例子和Dynamic.cs,也找到了《Linq to SQL Dynamic 動態(tài)查詢》,有更細(xì)致的例子,可惜Dynamic.cs也是不能使用like的,恨?。?/p>

return Consulting.Instance.UserT_TractInfo.Where(  "b_number == @0","P(2007)031").OrderByDescending(t => t.ID).  Skip(startIndex - 1).Take(pageSize).ToList();

代碼很容易,但沒什么用:(

LINQ模糊查詢實(shí)現(xiàn)階段三:

這里放出核心代碼,很容易看懂,簡單就是美!

searchPredicate = PredicateExtensions.  True<UserT_TractInfo>();  foreach (string key in nvcParam)  {  string condition = string.Empty;  switch (key)  {    case "b_number":   condition = nvcParam[key];   searchPredicate = searchPredicate.And(u =>   u.B_number.Contains(condition));   break;    case "b_address":   condition = nvcParam[key];   searchPredicate = searchPredicate.And(u =>   u.B_address.Contains(condition));   break;    case "b_canton":   condition = nvcParam[key];   searchPredicate = searchPredicate.And(u =>   u.B_canton.Contains(condition));   break;    case "a_status":   condition = nvcParam[key];   searchPredicate = searchPredicate.And(u =>   u.A_status.ToString().Contains(condition));   break;    case "b_area":   condition = nvcParam[key];   searchPredicate = searchPredicate.And(u =>   u.B_area.Contains(condition));   break;    case "c_clinchdate":   condition = nvcParam[key];   searchPredicate = searchPredicate.And(u =>   u.C_clinchdate.Contains(condition));   break;    default:   break;  }  }   return Consulting.Instance.UserT_TractInfo.  Where(searchPredicate).OrderByDescending(t => t.ID).  Skip(startIndex - 1).Take(pageSize).ToList();

下面是我寫了注釋后的PredicateExtensions,我說不清楚構(gòu)造函數(shù)的True和False具體是怎么起作用的,但結(jié)果就是我的注釋那樣,在復(fù)合查詢寫條件時很重要(不過目前全寫AND就完成復(fù)合查詢了,我還沒搞多關(guān)鍵詞OR的那種):

/// <summary>  /// 構(gòu)造函數(shù)使用True時:單個AND有效,多個AND有效;  ///單個OR無效,多個OR無效;混合時寫在AND后的OR有效  /// 構(gòu)造函數(shù)使用False時:單個AND無效,多個AND無效;  ///單個OR有效,多個OR有效;混合時寫在OR后面的AND有效  /// </summary>  public static class PredicateExtensions  {  public static Expression<Func<T,   bool>> True<T>() { return f => true; }   public static Expression<Func<T, bool>> False<T>() {   return f => false; }  public static Expression<Func<T, bool>>   Or<T>(this Expression<Func<T, bool>> expression1,   Expression<Func<T, bool>> expression2)  {  var invokedExpression = Expression.Invoke(  expression2, expression1.Parameters.Cast<Expression>());   return Expression.Lambda<Func<T, bool>>(  Expression.Or(expression1.Body,   invokedExpression),   expression1.Parameters);  }   public static Expression<Func<T, bool>> And<T>(  this Expression<Func<T, bool>> expression1,   Expression<Func<T, bool>> expression2)  {  var invokedExpression =   Expression.Invoke(expression2,   expression1.Parameters.Cast<Expression>());   return Expression.Lambda<Func<T, bool>>  (Expression.And(expression1.Body, invokedExpression),   expression1.Parameters);  }  }

到此,相信大家對“LINQ模糊查詢怎么使用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)
AI