Click here to Skip to main content
15,886,199 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.3K   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 CommonGenius.Linq.Test {
    using System.Data.Common;

    class ConnectionWrapper : DbConnection {
        private readonly DbConnection _connection;

        public ConnectionWrapper(DbConnection connection) {
            _connection = connection;
        }

        protected override DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel) {
            return _connection.BeginTransaction(isolationLevel);
        }

        public override void ChangeDatabase(string databaseName) {
            _connection.ChangeDatabase(databaseName);
        }

        public override void Close() {
            _connection.Close();
        }

        public override string ConnectionString {
            get {
                return _connection.ConnectionString;
            }
            set {
                _connection.ConnectionString = value;
            }
        }

        protected override DbCommand CreateDbCommand() {
            return new CommandWrapper(_connection.CreateCommand());
        }

        public override string DataSource {
            get { return _connection.DataSource; }
        }

        public override string Database {
            get { return _connection.Database; }
        }

        public override void Open() {
            _connection.Open();
        }

        public override string ServerVersion {
            get { return _connection.ServerVersion; }
        }

        public override System.Data.ConnectionState State {
            get { return _connection.State; }
        }
    }

    class CommandWrapper : DbCommand {

        private readonly DbCommand _command;

        public CommandWrapper(DbCommand command) {
            _command = command;
        }

        public override void Cancel() {
            _command.Cancel();
        }

        public override string CommandText {
            get {
                return _command.CommandText;
            }
            set {
                System.Diagnostics.Trace.WriteLine(value);
                _command.CommandText = value;
            }
        }

        public override int CommandTimeout {
            get {
                return _command.CommandTimeout;
            }
            set {
                _command.CommandTimeout = value;
            }
        }

        public override System.Data.CommandType CommandType {
            get {
                return _command.CommandType;
            }
            set {
                _command.CommandType = value;
            }
        }

        protected override DbParameter CreateDbParameter() {
            return _command.CreateParameter();
        }

        protected override DbConnection DbConnection {
            get {
                return _command.Connection;
            }
            set {
                _command.Connection = value;
            }
        }

        protected override DbParameterCollection DbParameterCollection {
            get { return _command.Parameters; }
        }

        protected override DbTransaction DbTransaction {
            get {
                return _command.Transaction;
            }
            set {
                _command.Transaction = value;
            }
        }

        public override bool DesignTimeVisible {
            get {
                return _command.DesignTimeVisible;
            }
            set {
                _command.DesignTimeVisible = value;
            }
        }

        protected override DbDataReader ExecuteDbDataReader(System.Data.CommandBehavior behavior) {
            return _command.ExecuteReader(behavior);
        }

        public override int ExecuteNonQuery() {
            return _command.ExecuteNonQuery();
        }

        public override object ExecuteScalar() {
            return _command.ExecuteScalar();
        }

        public override void Prepare() {
            _command.Prepare();
        }

        public override System.Data.UpdateRowSource UpdatedRowSource {
            get {
                return _command.UpdatedRowSource;
            }
            set {
                _command.UpdatedRowSource = value;
            }
        }
    }
}

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