Tools/LinqToSqlExtensions.cs
/// <summary>
/// Batches together multiple IQueryable queries into a single DbCommand and returns all data in
/// a single roundtrip to the database.
/// </summary>
/// <param name="context">The DataContext to execute the batch select against.</param>
/// <param name="queries">Represents a collections of SELECT queries to execute.</param>
/// <returns>Returns an IMultipleResults object containing all results.</returns>
Batches together multiple IQueryable queries into a single DbCommand and returns all data in
a single roundtrip to the database.
Batches together multiple IQueryable queries into a single DbCommand and returns all data in a single roundtrip to the database .
public static IMultipleResults SelectMutlipleResults( this DataContext context, params IQueryable[] queries )
		{
            if (queries == null || queries.Length == 0)
                return null;
			var commandList = new List<DbCommand>();
			foreach ( IQueryable query in queries )
			{
				var command = context.GetCommand( query );
				commandList.Add( command );
			}
			SqlCommand batchCommand = CombineCommands( commandList );
			batchCommand.Connection = context.Connection as SqlConnection;
			DbDataReader dr = null;
			if ( batchCommand.Connection.State == ConnectionState.Closed )
			{
				batchCommand.Connection.Open();
				dr = batchCommand.ExecuteReader( CommandBehavior.CloseConnection );
			}
			else
			{
				dr = batchCommand.ExecuteReader();
			}
			IMultipleResults mr = context.Translate( dr );
			return mr;
		}
public static IMultipleResults SelectMutlipleResults ( this DataContext context , params IQueryable [ ] queries ) { if ( queries == null || queries . Length == 0 ) return null ; var commandList = new List < DbCommand > ( ) ; foreach ( IQueryable query in queries ) { var command = context . GetCommand ( query ) ; commandList . Add ( command ) ; } SqlCommand batchCommand = CombineCommands ( commandList ) ; batchCommand . Connection = context . Connection as SqlConnection ; DbDataReader dr = null ; if ( batchCommand . Connection . State == ConnectionState . Closed ) { batchCommand . Connection . Open ( ) ; dr = batchCommand . ExecuteReader ( CommandBehavior . CloseConnection ) ; } else { dr = batchCommand . ExecuteReader ( ) ; } IMultipleResults mr = context . Translate ( dr ) ; return mr ; }
Batches together multiple IQueryable queries into a single DbCommand and returns all data in
a single roundtrip to the database.
Batches together multiple IQueryable queries into a single DbCommand and returns all data in a single roundtrip to the database .
Returns an IMultipleResults object containing all results.
Returns an IMultipleResults object containing all results .
DataContext
this
context
The DataContext to execute the batch select against.
The DataContext to execute the batch select against .
queries
Represents a collections of SELECT queries to execute.
Represents a collections of SELECT queries to execute .