拼接linq查询表达式

拼接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);


原生的linq暂还不支持select.And select.Or 这种写法,要借助这个扩展类LinqHelper,如下:

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;
            }
        }
    }
}

这样的话做查询处理就非常方便

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值