拼接linq查询表达式
来源:互联网 发布:淘宝投资要多少钱 编辑:程序博客网 时间:2024/06/03 03:58
拼接linq查询表达式
1 比如在做数据库查询时面对前端可能多样的查询条件是,有时拼接查询条件能很方便的处理这种情况,如下代码所示
public GListResult<Entity.SysRole> GetList(Hashtable ht, int skip, int top) { System.Linq.Expressions.Expression<Func<Entity.SysRole, bool>> select = ff => ff.Id != 0; System.Linq.Expressions.Expression<Func<Entity.SysRole, int>> orderBy = ob => ob.Id; System.Linq.Expressions.Expression<Func<Entity.SysRole, bool>> subselect = ff => ff.Id == 0; if (ht != null && ht.Count > 0) { if (ht.Contains("Id")) { select = select.And(ff => ff.Id == HQ.Job.Tools.StrHelper.ToInt32(ht["Id"].ToString())); } if (ht.Contains("RoleID")) { string[] arrRoleId = ht["RoleID"].ToString().Split(','); foreach (string item in arrRoleId) { subselect=subselect.Or(ff => ff.Id == HQ.Job.Tools.StrHelper.ToInt32(item.ToString())); } select = select.And(subselect); } <pre name="code" class="csharp"><span style="white-space:pre"></span>if (ht.Contains("Name")) { select = select.And(ff => ff.Name == HQ.Job.Tools.StrHelper.Format(ht["Name"].ToString())); }} var g = dal.GetAll(select, null, skip, top, orderBy); return g; }
dal.GetAll 的接口如下
GListResult<T> GetAll<TKey>(Expression<Func<T, bool>> query, Expression<Func<T, T>> select, int skip, int top, Expression<Func<T, TKey>> order = null, bool isDesOrder = false);
using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using System.Text;using System.Threading.Tasks;namespace HQ.Backstage.Business{ public static class LinqHelper { public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> one, Expression<Func<T, bool>> another) { var candidateExpr = Expression.Parameter(typeof(T), "candidate"); var parameterReplacer = new ParameterReplacer(candidateExpr); var left = parameterReplacer.Replace(one.Body); var right = parameterReplacer.Replace(another.Body); var body = Expression.And(left, right); return Expression.Lambda<Func<T, bool>>(body, candidateExpr); } public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> one, Expression<Func<T, bool>> another) { var candidateExpr = Expression.Parameter(typeof(T), "candidate"); var parameterReplacer = new ParameterReplacer(candidateExpr); var left = parameterReplacer.Replace(one.Body); var right = parameterReplacer.Replace(another.Body); var body = Expression.Or(left, right); return Expression.Lambda<Func<T, bool>>(body, candidateExpr); } internal class ParameterReplacer : ExpressionVisitor { public ParameterReplacer(ParameterExpression paramExpr) { this.ParameterExpression = paramExpr; } public ParameterExpression ParameterExpression { get; private set; } public Expression Replace(Expression expr) { return this.Visit(expr); } protected override Expression VisitParameter(ParameterExpression p) { return this.ParameterExpression; } } }}
这样的话做查询处理就非常方便
1 0
- 拼接linq查询表达式
- 拼接linq查询表达式
- wcf ria 解决linq表达式拼接动态查询的类PredicateBuilder
- 查询表达式(LINQ)简介
- 查询表达式(LINQ)简介
- LINQ 查询表达式
- 查询表达式(LINQ)简介
- LINQ 查询表达式学习
- 查询表达式Linq
- 查询表达式(LINQ)简介
- 查询表达式(LINQ)简介
- LINQ查询表达式
- LINQ 查询表达式
- LINQ查询表达式
- linq 动态拼接查询条件 扩展方法
- LINQ To SQL 拼接查询条件
- linq 动态拼接查询条件 扩展方法
- 动态拼接LINQ查询条件的解决方案
- 《正确去掉Win7快捷方式小箭头》
- C语言的整型溢出问题
- 【BZOJ3064】【Tyvj1518】CPU监控 裸线段树
- 拼接linq查询表达式
- VLC架构及流程分析
- 拼接linq查询表达式
- ViewPager相互嵌套,里层ViewPager无法滑动
- swift语言注册非免费苹果账号iOS游戏框架Sprite Kit基础教程
- 各知识点2
- Servlet
- EL表达式中fn函数
- GPU着色器实现实时滤镜
- Android notification的使用介绍
- sshpass-Linux命令之非交互SSH密码验证