zoukankan      html  css  js  c++  java
  • entityframework多条件查询类

    entityframework多条件查询类

    var dataaccess = new BaseAccess();
                int totalCount = 0;
                var paramS = new OrderModelField[1];
                paramS[0].IsDesc = true;
                paramS[0].PropertyName = "B_Email";
                Expression<Func<Rm_Sec_UserData, bool>> expr = PredicateExtensions.True<Rm_Sec_UserData>().And(t => t.F_Email.Equals(pUserEmail)).And(t => t.F_UserPass == pPassword);
    
                var list = dataaccess.GetListPaged(1, 2,expr, out totalCount, paramS);
    
     //
    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>> And<T>(this Expression<Func<T, bool>> exp_left, Expression<Func<T, bool>> exp_right)
        {
            var candidateExpr = Expression.Parameter(typeof(T), "candidate");
            var parameterReplacer = new ParameterReplacer(candidateExpr);
    
            var left = parameterReplacer.Replace(exp_left.Body);
            var right = parameterReplacer.Replace(exp_right.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>> exp_left, Expression<Func<T, bool>> exp_right)
        {
            var candidateExpr = Expression.Parameter(typeof(T), "candidate");
            var parameterReplacer = new ParameterReplacer(candidateExpr);
    
            var left = parameterReplacer.Replace(exp_left.Body);
            var right = parameterReplacer.Replace(exp_right.Body);
            var body = Expression.Or(left, right);
    
            return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
        }
    }
    /// <summary>
    /// 统一ParameterExpression
    /// </summary>
    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;
        }
    }
  • 相关阅读:
    C Python类型互换
    C、C++中如何成功嵌入python
    常见Style 对象属性值
    转: , , 的区别
    dom4j: 用dom4j生成xml后第二行空行的问题
    dom4j: 生成的XML文件根节点 xmlns="" 的问题
    android
    android studio
    android studio
    FFmpeg编译: undefined reference to 'av_frame_alloc()'
  • 原文地址:https://www.cnblogs.com/renzaijianghu/p/4216835.html
Copyright © 2011-2022 走看看