Linq To Entity 查询条件扩展

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linq To Entity 查询条件扩展相关的知识,希望对你有一定的参考价值。

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Linq.Expressions;
  5 using System.Web;
  6 
  7 namespace Test
  8 {
  9     /// <summary>
 10     /// 统一ParameterExpression
 11     /// </summary>
 12     public class ParameterReplacer : ExpressionVisitor
 13     {
 14         public ParameterReplacer(ParameterExpression paramExpr)
 15         {
 16             ParameterExpression = paramExpr;
 17         }
 18 
 19         public ParameterExpression ParameterExpression { get; private set; }
 20 
 21         public Expression Replace(Expression expr)
 22         {
 23             return Visit(expr);
 24         }
 25 
 26         protected override Expression VisitParameter(ParameterExpression p)
 27         {
 28             return ParameterExpression;
 29         }
 30     }
 31     /// <summary>
 32     /// Predicate扩展
 33     /// </summary>
 34     public static class PredicateExtensionses
 35     {
 36         /// <summary>
 37         /// 
 38         /// </summary>
 39         /// <typeparam name="T"></typeparam>
 40         /// <returns></returns>
 41         public static Expression<Func<T, bool>> True<T>() { return f => true; }
 42         /// <summary>
 43         /// 
 44         /// </summary>
 45         /// <typeparam name="T"></typeparam>
 46         /// <returns></returns>
 47         public static Expression<Func<T, bool>> False<T>() { return f => false; }
 48         /// <summary>
 49         /// 
 50         /// </summary>
 51         /// <typeparam name="T"></typeparam>
 52         /// <param name="expLeft"></param>
 53         /// <param name="expRight"></param>
 54         /// <returns></returns>
 55         public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expLeft, Expression<Func<T, bool>> expRight)
 56         {
 57             var candidateExpr = Expression.Parameter(typeof(T), "candidate");
 58             var parameterReplacer = new ParameterReplacer(candidateExpr);
 59 
 60             var left = parameterReplacer.Replace(expLeft.Body);
 61             var right = parameterReplacer.Replace(expRight.Body);
 62             var body = Expression.And(left, right);
 63 
 64             return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
 65         }
 66         /// <summary>
 67         /// 
 68         /// </summary>
 69         /// <typeparam name="T"></typeparam>
 70         /// <param name="expLeft"></param>
 71         /// <param name="expRight"></param>
 72         /// <returns></returns>
 73         public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expLeft, Expression<Func<T, bool>> expRight)
 74         {
 75             var candidateExpr = Expression.Parameter(typeof(T), "candidate");
 76             var parameterReplacer = new ParameterReplacer(candidateExpr);
 77 
 78             var left = parameterReplacer.Replace(expLeft.Body);
 79             var right = parameterReplacer.Replace(expRight.Body);
 80             var body = Expression.Or(left, right);
 81 
 82             return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
 83         }
 84     }
 85     /// <summary>
 86     /// Queryable扩展
 87     /// </summary>
 88     public static class QueryableExtensions
 89     {
 90         /// <summary>
 91         /// 
 92         /// </summary>
 93         /// <typeparam name="T"></typeparam>
 94         /// <param name="queryable"></param>
 95         /// <param name="propertyName"></param>
 96         /// <returns></returns>
 97         public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName)
 98         {
 99             return OrderBy(queryable, propertyName, false);
100         }
101         /// <summary>
102         /// OrderBy
103         /// </summary>
104         /// <typeparam name="T">实体</typeparam>
105         /// <param name="queryable">条件</param>
106         /// <param name="propertyName">属性名称</param>
107         /// <param name="desc">是否降序</param>
108         /// <returns></returns>
109         public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName, bool desc)
110         {
111             var param = Expression.Parameter(typeof(T));
112             var body = Expression.Property(param, propertyName);
113             dynamic keySelector = Expression.Lambda(body, param);
114             return desc ? Queryable.OrderByDescending(queryable, keySelector) : Queryable.OrderBy(queryable, keySelector);
115         }
116     }
117 }

 

以上是关于Linq To Entity 查询条件扩展的主要内容,如果未能解决你的问题,请参考以下文章

初识Linq to Entity

.AsExpandable 在 Linq to Entity

无法在 linq to entity 查询中构造实体或复杂类型“x”

在 linq to entity 中查询实体的 ICollection

使用 linq/Entity Framework 查询多对多关系。代码优先

优化多个 LINQ to Entity Framework 查询