Click here to Skip to main content
15,885,686 members
Articles / Programming Languages / C#

Modifying LINQ Expressions with Rewrite Rules

Rate me:
Please Sign up or sign in to vote.
4.98/5 (29 votes)
18 Mar 2008CPOL24 min read 75.6K   825   64  
Rewriting query expressions is a simple and yet safe and powerful technique to modify queries dynamically at runtime.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Rewrite;
using System.Linq.Expressions;
using System.Data.Linq.SqlClient;

using RewriteSql;

namespace RewriteDemo {
    class UserInput {
        internal static PropertyInfo[] CustomerProps {
            get {
                // exclude EntitySets
                return typeof( Customer ).GetProperties().Where( p => !p.PropertyType.IsGenericType ).ToArray();
            }
        }

        internal static PropertyInfo[] CustomerExtProps {
            // exclude EntitySets
            get { return typeof( CustomerExt ).GetProperties().Where( p => !p.PropertyType.IsGenericType).ToArray(); }
        }

        internal static string[] CompareOps( Type type ) {
            IEnumerable<string> ops = CompareOpDecoder.GetCompareOpNames( type );
            if( ops != null )
                return ops.ToArray();
            return null;
        }

        public static Expression<Func<Customer, bool>> MakeCityFilter( string value ) {
            if( string.IsNullOrEmpty( value ) )
                return null; // ---------->>>>>>>>>>>>>
            value = value.ToUpper();

            Expression<Func<Customer, bool>> expr = c => SqlMethods.Like( c.City.ToUpper(), value );

            // this is done solely for better readability
            expr = SimpleRewriter.ApplyOnce( expr,
                Rule.Create( () => value, EvaluateLiteral.CreateRhs( value ) ) );

            return expr;
        }

        public static Expression<Func<Customer, bool>> MakeOrdersInYearFilter( string yearStr, string countStr ) {
            int cnt, year;
            if( !int.TryParse( countStr, out cnt ) || !int.TryParse( yearStr, out year ) ) 
                return null; // ---------->>>>>>>>>>>>>

            Expression<Func<Customer, bool>> expr 
                = c => c.Orders.Count( o => o.OrderDate.HasValue && o.OrderDate.Value.Year == year ) >= cnt;
            var rwr = new SimpleRewriter( expr );
            Expression<Func<int>> yearLhs = () => year;
            rwr.ApplyOnce( new Rule( yearLhs, EvaluateLiteral.CreateRhs( typeof( int ), year ) ) );
            Expression<Func<int>> cntLhs = () => cnt;
            rwr.ApplyOnce( new Rule( cntLhs, EvaluateLiteral.CreateRhs( typeof( int ), cnt ) ) );
            
            return (Expression<Func<Customer, bool>>)rwr.Expression;
        }

        public static Expression<Func<Customer, bool>> MakeGerericFilter( string propName, string opName, string value ) {
            if( string.IsNullOrEmpty( propName) || string.IsNullOrEmpty( opName) || string.IsNullOrEmpty( value))
                return null; // ---------->>>>>>>>>>>>>

            return GenericBinFilter<Customer>.Create( propName, opName, value );
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions