zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    

      

  • 相关阅读:
    CS224n, lec 10, NMT & Seq2Seq Attn
    CS231n笔记 Lecture 11, Detection and Segmentation
    CS231n笔记 Lecture 10, Recurrent Neural Networks
    CS231n笔记 Lecture 9, CNN Architectures
    CS231n笔记 Lecture 8, Deep Learning Software
    CS231n笔记 Lecture 7, Training Neural Networks, Part 2
    pytorch坑点排雷
    Sorry, Ubuntu 17.10 has experienced an internal error
    VSCode配置python插件
    tmux配置与使用
  • 原文地址:https://www.cnblogs.com/blackice/p/2851424.html
Copyright © 2011-2022 走看看