本篇内容主要讲解“LINQ模糊查询怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“LINQ模糊查询怎么使用”吧!
网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了耿马免费建站欢迎大家使用!
LINQ模糊查询实现的多条件复合搜索效果如下图:
LINQ模糊查询实现阶段一:
利用Lambda表达式树可以进行动态查询。写了个方法进行复合查询,动态组合条件,生成Lambda表达式。
////// 这个方法带分页功能,通过输入的键值对NVC进行复合查询 /// ListGetPagedObjectsByNVC( int startIndex, int pageSize, NameValueCollection nvc, bool isAnd) { IQueryable 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) //当对象存在此属性时(因为键值对可能还有很多其他的参数,例如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组合了各个有效的键值对对应的conditionExpression, //复合查询最重要的组合工作就这么完了 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 (whereCallExpression). OrderByDescending(t => t.ID).Skip(startIndex - 1). Take(pageSize).ToList();//查询出结果 } else { return Consulting.Instance.UserT_TractInfo. OrderByDescending(t => t.ID).Skip(startIndex - 1). Take(pageSize).ToList(); //如果没有有效键值对,则返回全部结果 } }
搞了半天本来很兴奋的,之后才知道Lambda表达式是写不出.Contains()的,我的心瓦凉瓦凉的。
LINQ模糊查询实现阶段二:
虽然李永京的文章没给我多少帮助,但它后面有个回复很有价值:“用微软提供的System.Linq.Dynamic方便点。”很快找到了对应例子和Dynamic.cs,也找到了《Linq to SQL Dynamic 动态查询》,有更细致的例子,可惜Dynamic.cs也是不能使用like的,恨啊!
return Consulting.Instance.UserT_TractInfo.Where( "b_number == @0","P(2007)031").OrderByDescending(t => t.ID). Skip(startIndex - 1).Take(pageSize).ToList();
代码很容易,但没什么用:(
LINQ模糊查询实现阶段三:
这里放出核心代码,很容易看懂,简单就是美!
searchPredicate = PredicateExtensions. True(); 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,我说不清楚构造函数的True和False具体是怎么起作用的,但结果就是我的注释那样,在复合查询写条件时很重要(不过目前全写AND就完成复合查询了,我还没搞多关键词OR的那种):
////// 构造函数使用True时:单个AND有效,多个AND有效; ///单个OR无效,多个OR无效;混合时写在AND后的OR有效 /// 构造函数使用False时:单个AND无效,多个AND无效; ///单个OR有效,多个OR有效;混合时写在OR后面的AND有效 /// public static class PredicateExtensions { public static Expression> True () { return f => true; } public static Expression > False () { return f => false; } public static Expression > Or (this Expression > expression1, Expression > expression2) { var invokedExpression = Expression.Invoke( expression2, expression1.Parameters.Cast ()); return Expression.Lambda >( Expression.Or(expression1.Body, invokedExpression), expression1.Parameters); } public static Expression > And ( this Expression > expression1, Expression > expression2) { var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast ()); return Expression.Lambda > (Expression.And(expression1.Body, invokedExpression), expression1.Parameters); } }
到此,相信大家对“LINQ模糊查询怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!