I want to get the translated Linq query programmatically and do some stuff with that Sql syntax.
Suppose this is my code:
public class MyApiControlle
I think these posts may help you:
1) How to view LINQ Generated SQL statements?
2) How do I view the SQL generated by the Entity Framework?
Couple of examples (from the above):
var sql = ((System.Data.Objects.ObjectQuery)objs).ToTraceString();
var sql = objs.ToString();
Documentation on .ToTraceString()
can be found here:
MSDN trace string documentation
another option if you are using Entity Framework 6 is use the new feature to logging whats is happen, you can get the t-sql and the query time:
Logging and Intercepting Database Operations
using (var context = new BlogContext())
{
context.Database.Log = Console.Write; //here, you can write this info to a text file for example.
// Your code here...
}
You can call the ToString()
method on objs
. This will result in a call to the ToTraceString
method that returns the executed SQL:
string sql = objs.ToString();