Click here to Skip to main content
15,894,955 members
Articles / Programming Languages / XML

QueryMap: Custom Translation of LINQ Expressions

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
22 Apr 2012Ms-PL9 min read 36.4K   441   7  
QueryMap allows you to pre-translate a LINQ expression into a form that the underlying query provider (such as LINQ to SQL) can understand.
namespace Overboard.Linq {
    using System;
    using System.Linq.Expressions;

    /// <summary>
    /// Provides a set of <see langword="static"/> factory methods for creating instances of <see cref="ExpressionMethod{TDelegate}"/> while taking full advantage of type inference.
    /// </summary>
    public static class ExpressionMethod {
        /// <summary>
        /// Create an instance of <see cref="ExpressionMethod{TDelegate}" /> from the underlying expression tree.
        /// </summary>
        /// <typeparam name="TResult">The type of the return value of the expression to be encapsulated.</typeparam>
        /// <param name="expression">The expression to be encapsulated.</param>
        /// <returns>An <see cref="ExpressionMethod{TDelegate}" /> which encapsulates an expression tree and its corresponding delegate.</returns>
        public static ExpressionMethod<Func<TResult>> Create<TResult>(Expression<Func<TResult>> expression) {
            return new ExpressionMethod<Func<TResult>>(expression);
        }

        /// <summary>
        /// Create an instance of <see cref="ExpressionMethod{TDelegate}" /> from the underlying expression tree.
        /// </summary>
        /// <typeparam name="T">The type of the parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="TResult">The type of the return value of the expression to be encapsulated.</typeparam>
        /// <param name="expression">The expression to be encapsulated.</param>
        /// <returns>An <see cref="ExpressionMethod{TDelegate}" /> which encapsulates an expression tree and its corresponding delegate.</returns>
        public static ExpressionMethod<T, TResult> Create<T, TResult>(Expression<Func<T, TResult>> expression) {
            return new ExpressionMethod<T, TResult>(expression);
        }

        /// <summary>
        /// Create an instance of <see cref="ExpressionMethod{TDelegate}" /> from the underlying expression tree.
        /// </summary>
        /// <typeparam name="T1">The type of the first parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T2">The type of the second parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="TResult">The type of the return value of the expression to be encapsulated.</typeparam>
        /// <param name="expression">The expression to be encapsulated.</param>
        /// <returns>An <see cref="ExpressionMethod{TDelegate}" /> which encapsulates an expression tree and its corresponding delegate.</returns>
        public static ExpressionMethod<Func<T1, T2, TResult>> Create<T1, T2, TResult>(Expression<Func<T1, T2, TResult>> expression) {
            return new ExpressionMethod<Func<T1, T2, TResult>>(expression);
        }

        /// <summary>
        /// Create an instance of <see cref="ExpressionMethod{TDelegate}" /> from the underlying expression tree.
        /// </summary>
        /// <typeparam name="T1">The type of the first parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T2">The type of the second parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T3">The type of the third parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="TResult">The type of the return value of the expression to be encapsulated.</typeparam>
        /// <param name="expression">The expression to be encapsulated.</param>
        /// <returns>An <see cref="ExpressionMethod{TDelegate}" /> which encapsulates an expression tree and its corresponding delegate.</returns>
        public static ExpressionMethod<Func<T1, T2, T3, TResult>> Create<T1, T2, T3, TResult>(Expression<Func<T1, T2, T3, TResult>> expression) {
            return new ExpressionMethod<Func<T1, T2, T3, TResult>>(expression);
        }

        /// <summary>
        /// Create an instance of <see cref="ExpressionMethod{TDelegate}" /> from the underlying expression tree.
        /// </summary>
        /// <typeparam name="T1">The type of the first parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T2">The type of the second parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T3">The type of the third parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T4">The type of the fourth parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="TResult">The type of the return value of the expression to be encapsulated.</typeparam>
        /// <param name="expression">The expression to be encapsulated.</param>
        /// <returns>An <see cref="ExpressionMethod{TDelegate}" /> which encapsulates an expression tree and its corresponding delegate.</returns>
        public static ExpressionMethod<Func<T1, T2, T3, T4, TResult>> Create<T1, T2, T3, T4, TResult>(Expression<Func<T1, T2, T3, T4, TResult>> expression) {
            return new ExpressionMethod<Func<T1, T2, T3, T4, TResult>>(expression);
        }

        /// <summary>
        /// Create an instance of <see cref="ExpressionMethod{TDelegate}" /> from the underlying expression tree.
        /// </summary>
        /// <param name="expression">The expression to be encapsulated.</param>
        /// <returns>An <see cref="ExpressionMethod{TDelegate}" /> which encapsulates an expression tree and its corresponding delegate.</returns>
        public static ExpressionMethod<Action> Create(Expression<Action> expression) {
            return new ExpressionMethod<Action>(expression);
        }

        /// <summary>
        /// Create an instance of <see cref="ExpressionMethod{TDelegate}" /> from the underlying expression tree.
        /// </summary>
        /// <typeparam name="T">The type of the parameter of the expression to be encapsulated.</typeparam>
        /// <param name="expression">The expression to be encapsulated.</param>
        /// <returns>An <see cref="ExpressionMethod{TDelegate}" /> which encapsulates an expression tree and its corresponding delegate.</returns>
        public static ExpressionMethod<Action<T>> Create<T>(Expression<Action<T>> expression) {
            return new ExpressionMethod<Action<T>>(expression);
        }

        /// <summary>
        /// Create an instance of <see cref="ExpressionMethod{TDelegate}" /> from the underlying expression tree.
        /// </summary>
        /// <typeparam name="T1">The type of the first parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T2">The type of the second parameter of the expression to be encapsulated.</typeparam>
        /// <param name="expression">The expression to be encapsulated.</param>
        /// <returns>An <see cref="ExpressionMethod{TDelegate}" /> which encapsulates an expression tree and its corresponding delegate.</returns>
        public static ExpressionMethod<Action<T1, T2>> Create<T1, T2>(Expression<Action<T1, T2>> expression) {
            return new ExpressionMethod<Action<T1, T2>>(expression);
        }

        /// <summary>
        /// Create an instance of <see cref="ExpressionMethod{TDelegate}" /> from the underlying expression tree.
        /// </summary>
        /// <typeparam name="T1">The type of the first parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T2">The type of the second parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T3">The type of the third parameter of the expression to be encapsulated.</typeparam>
        /// <param name="expression">The expression to be encapsulated.</param>
        /// <returns>An <see cref="ExpressionMethod{TDelegate}" /> which encapsulates an expression tree and its corresponding delegate.</returns>
        public static ExpressionMethod<Action<T1, T2, T3>> Create<T1, T2, T3>(Expression<Action<T1, T2, T3>> expression) {
            return new ExpressionMethod<Action<T1, T2, T3>>(expression);
        }

        /// <summary>
        /// Create an instance of <see cref="ExpressionMethod{TDelegate}" /> from the underlying expression tree.
        /// </summary>
        /// <typeparam name="T1">The type of the first parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T2">The type of the second parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T3">The type of the third parameter of the expression to be encapsulated.</typeparam>
        /// <typeparam name="T4">The type of the fourth parameter of the expression to be encapsulated.</typeparam>
        /// <param name="expression">The expression to be encapsulated.</param>
        /// <returns>An <see cref="ExpressionMethod{TDelegate}" /> which encapsulates an expression tree and its corresponding delegate.</returns>
        public static ExpressionMethod<Action<T1, T2, T3, T4>> Create<T1, T2, T3, T4>(Expression<Action<T1, T2, T3, T4>> expression) {
            return new ExpressionMethod<Action<T1, T2, T3, T4>>(expression);
        }
    }
}

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 Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
United States United States
David Nelson has been programming in various languages for 17 years, and has been programming in .NET (C# and VB.NET) since 2003.
He is a MCTS in .NET 2.0 Web Applications, and is a moderator on the MSDN Forums (http://forums.microsoft.com/msdn).

Comments and Discussions