Linq to Entity query criteria extension

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.