Click here to Skip to main content
15,896,456 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 76.2K   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.Linq.Expressions;
using System.Text;
using System.Windows.Forms;

using Rewrite;
using RewriteSql;
using System.Data.Linq;

namespace RewriteDemo {
    public class CustomerExt {
        public string Address { get; set; }
        public string City { get; set; }
        public string CompanyName { get; set; }
        public string Country { get; set; }
        public string CustomerID { get; set; }
        public EntitySet<Order> Orders { get; set; }
        public string Region { get; set; }

        public int OrdersNo { get; set; }
    }

    public class DataAccess {
        public static string TheConnectString {
            get { return RewriteDemo.Properties.Settings.Default.NORTHWNDConnectionString; }
        }

        public static List<CustomerExt> GetCustomersSorted(
            Expression<Func<Customer, bool>> filter,
            Expression<Func<IQueryable<CustomerExt>, IOrderedQueryable<CustomerExt>>> orderByClause, 
            Action<string> showQuery ) 
        {
            try {
                using( NWindDataContext ctx = new NWindDataContext( TheConnectString ) ) {
                    Func<Customer, bool> dummyFilter = null;
                    Func<CustomerExt, object> dummySelector = null;

                    var q = from ext in
                                ( from c in ctx.Customers
                                  where dummyFilter( c )
                                  select new CustomerExt
                                  {
                                      OrdersNo = c.Orders.Count(),
                                      Address = c.Address,
                                      City = c.City,
                                      CompanyName = c.CompanyName,
                                      Country = c.Country,
                                      CustomerID = c.CustomerID,
                                      Orders = c.Orders,
                                      Region = c.Region
                                  } )
                            orderby dummySelector( ext )
                            select ext
                            ;

                    var e1 = WhereRewriter.Rewrite( q.Expression, x => dummyFilter( x ), filter );
                    var e2 = OrderByRewriter.Rewrite( e1, x => dummySelector( x ), orderByClause );

                    showQuery( e2.ToString() );

                    return WhereRewriter.RecreateQuery( q, e2 ).ToList();
                }
            }
            catch( Exception ex ) {
                MessageBox.Show( "Error in GetCustomersSorted(): " + ex.Message );
                return null;
            }
        }

    }
}

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