简体   繁体   English

如何显示Linq-To-SQL查询的SQL语句?

[英]How do you display the SQL statement of a Linq-To-SQL query?

I'm trying to retrieve the earliest DateTime value that has a specific date. 我正在尝试检索具有特定日期的最早的DateTime值。 The Holder instance has already been retrieved from the data context (without prefetching), now I need to search through the IO EntitySet (that has not been fetched). 已经从数据上下文中检索了Holder实例(没有预取),现在我需要搜索IO EntitySet(尚未取回)。

I'm doing this in a foreach loop for each holder so it takes a long time to retrieve the requested values. 我正在每个持有人的foreach循环中执行此操作,因此需要很长时间才能检索到所请求的值。 Indexing the table didn't help so I want to see the SQL statement in order to optimize the database. 索引表没有帮助,所以我想查看SQL语句以优化数据库。

This is the code that needs to be translated into SQL: 这是需要转换为SQL的代码:

DateTime? entryDateTime = (from io in Holder.IOs
                           where  io.IOStatus == "Entry" &&
                                  io.IODateTime.HasValue &&
                                  io.IODateTime.Value.Date == date.Date
                                  select io.IODateTime).Min();

I can't use LinqPad because the linq statement doesn't directly quesry the DataContext, but the IOs EntitySet. 不能使用LinqPad,因为linq语句不会直接查询DataContext,而是直接查询IOs EntitySet。

Also, if anyone has an idea on how to rewrite the linq statement in order to speed up the retrievals, please let me know. 另外,如果有人对如何重写linq语句有想法,以便加快检索速度,请告诉我。 I don't want to use LoadOptions as this would delay the app starting process. 我不想使用LoadOptions,因为这会延迟应用程序的启动过程。

Grab the query, stop on it in in the debugger. 抓住查询,在调试器中停止它。 YOu should be able to see it with a mouse over on the query object. 您应该能够将鼠标悬停在查询对象上。

I think your problem is the Min. 我认为您的问题是敏。 You get in SQL what you have in LINQ, and this is not what I would do in SQL. 用SQL可以得到LINQ的功能,而这不是我用SQL可以做到的。

I would gl with an order by and a Top 1 - which translates into First () in LINQ. 我会按顺序和前1来查询-在LINQ中将其翻译为First()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM