Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Modifying LINQ Expressions with Rewrite Rules

, 18 Mar 2008
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)

About the Author

Dmitri Raiko

Germany Germany
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 18 Mar 2008
Article Copyright 2008 by Dmitri Raiko
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid