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.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)

Share

About the Author

Dmitri Raiko

Germany Germany
No Biography provided

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