I want to get the translated Linq query programmatically and do some stuff with that Sql syntax.
Suppose this is my code:
public class MyApiController:ApiController
{
   public IQueryable<object> Get()
   {
      var objs=Context.Objexts.Where(m=>m.Id>10);
      return objs;
   }
}
I want to find and get the Sql syntax like:
SELECT * FROM dbo.Objexts where Id > 10
                Entity Framework Core uses Language-Integrated Query (LINQ) to query data from the database. LINQ allows you to use C# (or your . NET language of choice) to write strongly typed queries.
LINQ to Entities provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual Basic or Visual C#. Queries against the Entity Framework are represented by command tree queries, which execute against the object context.
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();
                        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... 
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With