Firstly, I would prefer to specify the columns manually, because I would like to minimize the influence of the base layers - the database schema, the query provider - on my application. But if you really want to do this, there is a little hacky way to achieve this.
When you use the entity infrastructure (database first):
var qs = ((ObjectQuery)q).ToTraceString(); var ds = new DataSet(); var da = new SqlDataAdapter(qs, new SqlConnection(((EntityConnection)context.Connection).StoreConnection.ConnectionString)); da.Fill(ds, "Result");
The idea is to catch the emitted SQL before it is actually executed, and use it to populate the DataTable
(in a DataSet
).
With Linq-to-sql, this is basically the same, just the way to get the command line and the connection string is different:
context.GetCommand(q).CommandText context.Connection.ConnectionString
With the first EF code:
q.ToString() context.Database.Connection.ConnectionString
source share