• Linq To Entity 查询条件扩展


        /// <summary>
        /// 统一ParameterExpression
        /// </summary>
        internal class ParameterReplacer : ExpressionVisitor
        {
            public ParameterReplacer(ParameterExpression paramExpr)
            {
                ParameterExpression = paramExpr;
            }
    
            public ParameterExpression ParameterExpression { get; private set; }
    
            public Expression Replace(Expression expr)
            {
                return Visit(expr);
            }
    
            protected override Expression VisitParameter(ParameterExpression p)
            {
                return ParameterExpression;
            }
        }
        /// <summary>
        /// Predicate扩展
        /// </summary>
        public static class PredicateExtensionses
        {
            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public static Expression<Func<T, bool>> True<T>() { return f => true; }
            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public static Expression<Func<T, bool>> False<T>() { return f => false; }
            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="expLeft"></param>
            /// <param name="expRight"></param>
            /// <returns></returns>
            public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expLeft, Expression<Func<T, bool>> expRight)
            {
                var candidateExpr = Expression.Parameter(typeof(T), "candidate");
                var parameterReplacer = new ParameterReplacer(candidateExpr);
    
                var left = parameterReplacer.Replace(expLeft.Body);
                var right = parameterReplacer.Replace(expRight.Body);
                var body = Expression.And(left, right);
    
                return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="expLeft"></param>
            /// <param name="expRight"></param>
            /// <returns></returns>
            public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expLeft, Expression<Func<T, bool>> expRight)
            {
                var candidateExpr = Expression.Parameter(typeof(T), "candidate");
                var parameterReplacer = new ParameterReplacer(candidateExpr);
    
                var left = parameterReplacer.Replace(expLeft.Body);
                var right = parameterReplacer.Replace(expRight.Body);
                var body = Expression.Or(left, right);
    
                return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
            }
        }
        /// <summary>
        /// Queryable扩展
        /// </summary>
        public static class QueryableExtensions
        {
            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="queryable"></param>
            /// <param name="propertyName"></param>
            /// <returns></returns>
            public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName)
            {
                return OrderBy(queryable, propertyName, false);
            }
            /// <summary>
            /// OrderBy
            /// </summary>
            /// <typeparam name="T">实体</typeparam>
            /// <param name="queryable">条件</param>
            /// <param name="propertyName">属性名称</param>
            /// <param name="desc">是否降序</param>
            /// <returns></returns>
            public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName, bool desc)
            {
                var param = Expression.Parameter(typeof(T));
                var body = Expression.Property(param, propertyName);
                dynamic keySelector = Expression.Lambda(body, param);
                return desc ? Queryable.OrderByDescending(queryable, keySelector) : Queryable.OrderBy(queryable, keySelector);
            }
        }
    

      

  • 相关阅读:
    getWidth() 和 getMeasuredWidth()的区别
    解决 win7 注册com组件失败问题
    Documentation/PCI/pci-iov-howto.txt
    Android Migrate Android Code
    struts2 18拦截器详解(九)
    使用Maven模板创建项目
    转换基于Maven的Web应用程序支持Eclipse IDE
    转换基于Maven的Java项目支持Eclipse IDE
    Eclipse构建Maven项目
    NetBeans IDE集成Maven
  • 原文地址:https://www.cnblogs.com/blackice/p/2851424.html
Copyright © 2020-2023  润新知