Is there any way to in .NET to see what LINQ query against database we are firing? For eg. I am wring a query in LINQ and I want to see that what SQL query is firing to communicate with database.
Is there any Visual Studio window or any other way?
Is there any way to in .NET to see what LINQ query against database we are firing? For eg. I am wring a query in LINQ and I want to see that what SQL query is firing to communicate with database.
Is there any Visual Studio window or any other way?
If you have a DbContext on which you firing your LINQ queries you can simply set the
DbContext.Database.Log
property to something like this:After this, every SQL query shows up in the Debug console from your Visual Studio with the category SQL.
You can also use SQL Server Profiler, Trace or Extended Events. First two are deprecated.
https://technet.microsoft.com/en-us/library/bb630354(v=sql.105).aspx
https://msdn.microsoft.com/en-us/library/ms181091.aspx
https://technet.microsoft.com/en-us/library/ms191006(v=sql.105).aspx
Are you looking for something like this
Then whenever a query is executed, you'll see an output like :
You can use the Log property of DataContext object. Also, it depends on the type of application you are using.
For Web Application:-
For Console Application:-
Apart from this you can also use the GetCommand method of
DataContext
class.Sql Server Profiler
is again an obvious option.You can use SQL Profiler to see how the LINQ expression is translated into SQL statement.