The following helper method executes commands asynchronously and returns a Task<IEnumerable<T>> where T is a type that receives the row information from the SqlDataReader rows. All works fine.
public static async Task<IEnumerable<T>> CommandExecuteReaderAsync<T>(SqlConnection conn, SqlTransaction trans, CommandType commandType, string commandText, IEnumerable<SqlParameter> parameters, CancellationToken token, int commandTimout)
{
using (var command = new SqlCommand(commandText, conn, trans))
{
command.CommandTimeout = commandTimout;
command.Parameters.AddRange(parameters.ToArray());
command.CommandType = commandType;
using (var sr = await command.ExecuteReaderAsync(token))
{
var dc = new System.Data.Linq.DataContext(conn);
return dc.Translate<T>(sr).ToList();
}
}
}
However, I'm not happy with my current approach of creating a new DataContext and using it's Translate<T> method.
Is there a better approach as the creation of the DataContext seems a bit excessive?
DataContextis? Ideally add it to the question. \$\endgroup\$