Click here to Skip to main content
12,634,403 members (25,550 online)
Click here to Skip to main content

Tagged as

Stats

48.1K views
649 downloads
63 bookmarked
Posted

Modifying LINQ Expressions with Rewrite Rules

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

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