开发者

How can i get sql query Equivalent to a linq expression

How can i find开发者_StackOverflow out when my program execute a linq expression what sql query has been executed?


Profiling is a sure way. Depending on the LINQ provider, this may also be inbuilt, for example with LINQ-to-SQL you can assign anything to .Log and it will write the queries as it does them. At the simplest level:

dataContext.Log = Console.Out; // for a LINQ-to-SQL DataContext
// TODO: now execute some queries against dataContext

Internally, we use a custom ADO.NET stack for this (and some other things). It looks like a SqlConnection (and LINQ-to-SQL is perfectly happy with it), but it does some logging and profiling for us on every command executed, so we can see where our long running operations are. I think Sam is looking to make this available to the public if you want.


Profiling the database would do it. Keep in mind that the statement won't be executed against the database until your code goes to interrogate the results of the query.


Have you tried LinqPad?

I believe it shows a SQL prototype of your query.


  1. you can use SQL Profiler, comes with SQL server install, and capture the statements as they execute
  2. you can use tool like LINQPad and run the LINQ against your data store and examine the SQL within the tool
  3. you can debug your application and get the SQL from the ObjectQuery.ToTraceString method


var res = somequery;
Console.WriteLine(res);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜