Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to find translated Linq to Entity query to Sql

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
like image 234
Mashtani Avatar asked Dec 18 '15 11:12

Mashtani


People also ask

Does Entity Framework use LINQ to SQL?

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.

Which is correct about LINQ to entities?

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.


2 Answers

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();
like image 62
Alex Avatar answered Oct 20 '22 07:10

Alex


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... 
}
like image 26
Julito Avellaneda Avatar answered Oct 20 '22 07:10

Julito Avellaneda