如何从编译的 Linq 查询中提取 Sql 命令
在正常(未编译)的 Linq to Sql 查询中,您可以通过以下代码从 IQueryable 中提取 SQLCommand:
SqlCommand cmd = (SqlCommand)table.Context.GetCommand(query);
是否可以对编译的查询执行相同的操作?
以下代码为我提供了编译查询的委托:
private static readonly Func<Data.DAL.Context, string, IQueryable<Word>> Query_Get =
CompiledQuery.Compile<Data.DAL.Context, string, IQueryable<Word>>(
(context, name) =>
from r in context.GetTable<Word>()
where r.Name == name
select r);
当我使用它生成 IQueryable 并尝试提取 SqlCommand 时,它似乎不起作用。调试代码时,我可以看到返回的 SqlCommand 有“非常”有用的 CommandText“SELECT NULL AS [EMPTY]”,
using (var db = new Data.DAL.Context())
{
IQueryable<Word> query = Query_Get(db, "word");
SqlCommand cmd = (SqlCommand)db.GetCommand(query);
Console.WriteLine(cmd != null ? cmd.CommandText : "Command Not Found");
}
毫无疑问,我在 google 中找不到有关此特定场景的任何内容尝试这不是一件常见的事情...
所以...有什么想法吗?
In normal (not compiled) Linq to Sql queries you can extract the SQLCommand from the IQueryable via the following code:
SqlCommand cmd = (SqlCommand)table.Context.GetCommand(query);
Is it possible to do the same for a compiled query?
The following code provides me with a delegate to a compiled query:
private static readonly Func<Data.DAL.Context, string, IQueryable<Word>> Query_Get =
CompiledQuery.Compile<Data.DAL.Context, string, IQueryable<Word>>(
(context, name) =>
from r in context.GetTable<Word>()
where r.Name == name
select r);
When i use this to generate the IQueryable and attempt to extract the SqlCommand it doesn't seem to work. When debugging the code I can see that the SqlCommand returned has the 'very' useful CommandText of 'SELECT NULL AS [EMPTY]'
using (var db = new Data.DAL.Context())
{
IQueryable<Word> query = Query_Get(db, "word");
SqlCommand cmd = (SqlCommand)db.GetCommand(query);
Console.WriteLine(cmd != null ? cmd.CommandText : "Command Not Found");
}
I can't find anything in google about this particular scenario, as no doubt it is not a common thing to attempt...
So.... Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 DataContext 日志:
You could use the DataContext log:
.NET 必须有一种方法,但您可以分析 SQL Server 数据库并捕获发送到它的 SQL。您可以从 SQL Management Studio 执行此操作。
There must be a way from .NET, but you can profile your SQL Server database and capture the SQL sent to it. You can do this from SQL Management Studio.