简体   繁体   中英

get generated sql from a compiled linq query

是否可以从已编译的linq查询中获取生成的SQL?

You can:

  1. Use the log property of the context to redirect the generated query to the output window of Visual Studio. link
  2. Or use the LINQ to SQL Debug Visualizer. link

Use LinqPad :

Or alternatively get use sql server profiler to watch the query. I know you used to be able to however over the query variable in debug and it would show you the query it is going to execute but I am not entirely sure if that still works (Definitely not on client side apps)

Thanks jfs, but the link in your option #1 is not good anymore. It is not showing any relevant article. Chris B's link to the MSDN article helped me.

Here is my solution since mine is not a Console application:

TextWriter tw = new StringWriter();
db.Log = tw;
IQueryable<Customer> custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

string output = tw.ToString();   
// output variable has the generate SQL now

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