简体   繁体   中英

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. 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).

I'm doing this in a foreach loop for each holder so it takes a long time to retrieve the requested values. Indexing the table didn't help so I want to see the SQL statement in order to optimize the database.

This is the code that needs to be translated into 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.

Also, if anyone has an idea on how to rewrite the linq statement in order to speed up the retrievals, please let me know. I don't want to use LoadOptions as this would delay the app starting process.

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.

I would gl with an order by and a Top 1 - which translates into First () in LINQ.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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