1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Linq.Expressions;5 usingsystem.web;6 7 namespaceTest8 {9 /// <summary>Ten ///Unified ParameterExpression One /// </summary> A Public classParameterreplacer:expressionvisitor - { - Publicparameterreplacer (parameterexpression paramexpr) the { -ParameterExpression =paramexpr; - } - + PublicParameterExpression ParameterExpression {Get;Private Set; } - + Publicexpression Replace (expression expr) A { at returnVisit (expr); - } - - protected OverrideExpression visitparameter (parameterexpression p) - { - returnparameterexpression; in } - } to /// <summary> + ///predicate extension - /// </summary> the Public Static classpredicateextensionses * { $ /// <summary>Panax Notoginseng /// - /// </summary> the /// <typeparam name= "T" ></typeparam> + /// <returns></returns> A Public StaticExpression<func<t,BOOL>> true<t> () {returnf =true; } the /// <summary> + /// - /// </summary> $ /// <typeparam name= "T" ></typeparam> $ /// <returns></returns> - Public StaticExpression<func<t,BOOL>> false<t> () {returnf =false; } - /// <summary> the /// - /// </summary>Wuyi /// <typeparam name= "T" ></typeparam> the /// <param name= "Expleft" ></param> - /// <param name= "Expright" ></param> Wu /// <returns></returns> - Public StaticExpression<func<t,BOOL>> and<t> ( ThisExpression<func<t,BOOL>> Expleft, Expression<func<t,BOOL>>expright) About { $ varcandidateexpr = Expression.parameter (typeof(T),"candidate"); - varParameterreplacer =NewParameterreplacer (candidateexpr); - - varleft =Parameterreplacer.replace (expleft.body); A varright =Parameterreplacer.replace (expright.body); + varBODY =Expression.and (left, right); the - returnExpression.lambda<func<t,BOOL>>(body, candidateexpr); $ } the /// <summary> the /// the /// </summary> the /// <typeparam name= "T" ></typeparam> - /// <param name= "Expleft" ></param> in /// <param name= "Expright" ></param> the /// <returns></returns> the Public StaticExpression<func<t,BOOL>> or<t> ( ThisExpression<func<t,BOOL>> Expleft, Expression<func<t,BOOL>>expright) About { the varcandidateexpr = Expression.parameter (typeof(T),"candidate"); the varParameterreplacer =NewParameterreplacer (candidateexpr); the + varleft =Parameterreplacer.replace (expleft.body); - varright =Parameterreplacer.replace (expright.body); the varBODY =expression.or (left, right);Bayi the returnExpression.lambda<func<t,BOOL>>(body, candidateexpr); the } - } - /// <summary> the ///queryable Extension the /// </summary> the Public Static classqueryableextensions the { - /// <summary> the /// the /// </summary> the /// <typeparam name= "T" ></typeparam>94 /// <param name= "queryable" ></param> the /// <param name= "PropertyName" ></param> the /// <returns></returns> the Public StaticIqueryable<t> orderby<t> ( ThisIqueryable<t> queryable,stringPropertyName)98 { About return(Queryable, PropertyName,false); - }101 /// <summary>102 /// by103 /// </summary>104 /// <typeparam name= "T" >Entity</typeparam> the /// <param name= "queryable" >conditions</param>106 /// <param name= "PropertyName" >Property name</param>107 /// <param name= "desc" >whether descending</param>108 /// <returns></returns>109 Public StaticIqueryable<t> orderby<t> ( ThisIqueryable<t> queryable,stringPropertyName,BOOLdesc) the {111 varparam = Expression.parameter (typeof(T)); the varBODY =expression.property (param, PropertyName);113 DynamicKeyselector =expression.lambda (body, param); the returnDesc?queryable.orderbydescending (queryable, keyselector): Queryable.orderby (queryable, keyselector); the } the }117}
Linq to Entity query criteria extension