Click here to Skip to main content
13,350,985 members (55,931 online)
Click here to Skip to main content

Stats

54K views
679 downloads
64 bookmarked
Posted 18 Mar 2008

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.Linq.Expressions;

using Rewrite;

namespace RewriteSql {
    public static class FilterBuilder {
        public static Expression<Func<TEntity, bool>> Or<TEntity>( params Expression<Func<TEntity, bool>>[] args ) {
            Func<TEntity, bool> p1 = null;
            Func<TEntity, bool> p2 = null;

            Expression<Func<TEntity, bool>> expr = e => p1( e );
            SimpleRewriter rwr = new SimpleRewriter( expr );
            bool noArgs = true;

            foreach( var arg in args.Where( y => y != null ) ) {
                noArgs = false;
                rwr.ApplyOnce( FilterRule<TEntity>( e => p1( e ), e => p2( e ) || p1( e ) ) );
                rwr.ApplyOnce( FilterRule( e => p2( e ), arg ) );
            }

            if( noArgs )
                return null; // ----------->>>>>>>>>>>>>>>>>>>>

            // remove p1
            Expression<Func<bool,TEntity,bool>> lhs = (p, e) => p || p1( e );
            Expression<Func<bool,TEntity,bool>> rhs = (p, e) => p;
            rwr.ApplyOnce( new Rule( lhs, rhs));

            return (Expression<Func<TEntity, bool>>)rwr.Expression;
        }

        public static Expression<Func<TEntity, bool>> And<TEntity>( params Expression<Func<TEntity, bool>>[] args ) {
            Func<TEntity, bool> p1 = null;
            Func<TEntity, bool> p2 = null;
            
            Expression<Func<TEntity, bool>> expr = e => p1( e);
            SimpleRewriter rwr = new SimpleRewriter( expr );
            bool noArgs = true;

            foreach( var arg in args.Where( y => y != null ) ) {
                noArgs = false;
                rwr.ApplyOnce( FilterRule<TEntity>( e => p1( e ), e => p2( e ) && p1( e ) ) );
                rwr.ApplyOnce( FilterRule( e => p2( e), arg));
            }

            if( noArgs )
                return null; // ----------->>>>>>>>>>>>>>>>>>>>

            // remove p1
            Expression<Func<bool, TEntity, bool>> lhs = ( x, e ) => x && p1( e );
            Expression<Func<bool, TEntity, bool>> rhs = ( x, e ) => x;
            rwr.ApplyOnce( new Rule( lhs, rhs ) );

            return (Expression<Func<TEntity, bool>>)rwr.Expression;
        }

        public static Expression<Func<TEntity, bool>> Not<TEntity>( Expression<Func<TEntity, bool>> arg ) {
            if( arg == null )
                return null;  // ---------->>>>>>>>>>>>>>>>>>>>>>>>

            Func<TEntity, bool> p = null;
            return SimpleRewriter.ApplyOnce<Func<TEntity,bool>>( e => !p( e ), FilterRule( e => p( e ), arg ) );
        }

        static public Rule FilterRule<TEntity>( Expression<Func<TEntity, bool>> lhs, Expression<Func<TEntity, bool>> rhs ) {
            return new Rule( lhs, rhs );
        }

    }
}

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)

Share

About the Author

Dmitri Raiko
Germany Germany
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 18 Mar 2008
Article Copyright 2008 by Dmitri Raiko
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid