我已经从数据上下文中使用了DbCommand。它需要一个 IQueryable。System.Data.Linq.DataContext.GetCommand(System.Linq.IQueryable)
摘要:表示要针对数据源执行的 SQL 语句或存储过程。为表示命令的特定于数据库的类提供基类。
public void GetSqlCommand()
{
const string sc2 = @"Server=SQLServerName;Database=DatabaseName;Trusted_Connection=True;";
using (var dc = new DataContext(sc2))
{
var query = dc.GetTable<Users>()
.Join(dc.GetTable<Phone>(),
x => x.UserId,
y => y.LastUserId,
(x, y) => new { User = x, Phone = y }).Select(x => x);
DbCommand command = dc.GetCommand(query);
Assert.IsNotNull(command.CommandText);
}
}
然后它给你这样的东西
SELECT [t0].[UserId], [t0].[Login], [t0].[FullName], [t0].[LastUserId], [t0].[LastDateTime], [t1].[PhoneId], [t1].[PhoneNumber], [t1].[LastUserId] AS [LastUserId2], [t1].[LastDateTime] AS [LastDateTime2]
FROM [dbo].[Users] AS [t0]
INNER JOIN [dbo].[Phone] AS [t1] ON ([t0].[UserId]) = [t1].[LastUserId]
更新您似乎想要框架用于从查询构建 SQL 的代码。这是 SQLProvider 的 MSFT 参考代码的链接,我认为您可以在其中找到答案。
SQLProvider
看看这个方法,它返回你传递的查询表达式的查询信息。Query Info 具有我上面提到的生成的 CommandText。
internal QueryInfo[] BuildQuery(Expression query, SqlNodeAnnotations annotations) {.. calls the private BuildQuery..}
private QueryInfo[] BuildQuery(ResultShape resultShape, Type resultType, SqlNode node, ReadOnlyCollection<Me.SqlParameter> parentParameters, SqlNodeAnnotations annotations) {...}
……
internal class QueryInfo {
SqlNode query;
string commandText;
ReadOnlyCollection<SqlParameterInfo> parameters;
ResultShape resultShape;
Type resultType;
internal QueryInfo(SqlNode query, string commandText, ReadOnlyCollection<SqlParameterInfo> parameters, ResultShape resultShape, Type resultType) {
this.query = query;
this.commandText = commandText;
this.parameters = parameters;
this.resultShape = resultShape;
this.resultType = resultType;
}
internal SqlNode Query {
get { return this.query; }
}
internal string CommandText {
get { return this.commandText; }
}
internal ReadOnlyCollection<SqlParameterInfo> Parameters {
get { return this.parameters; }
}
internal ResultShape ResultShape {
get { return this.resultShape; }
}
internal Type ResultType {
get { return this.resultType; }
}
}
您将看到 GetCommand 还调用Build Query Method 来生成 SQL,然后从第一个查询中获取命令文本
DbCommand IProvider.GetCommand(Expression query)
{
this.CheckDispose();
this.CheckInitialized();
if (query == null) {
throw Error.ArgumentNull("query");
}
this.InitializeProviderMode();
SqlNodeAnnotations annotations = new SqlNodeAnnotations();
QueryInfo[] qis = this.BuildQuery(query, annotations);
QueryInfo qi = qis[qis.Length - 1];
DbCommand cmd = this.conManager.Connection.CreateCommand();
cmd.CommandText = qi.CommandText;
cmd.Transaction = this.conManager.Transaction;
cmd.CommandTimeout = this.commandTimeout;
AssignParameters(cmd, qi.Parameters, null, null);
return cmd;
}