Search code examples
entity-frameworklinq-to-entities

In Linq to Entities can you convert an IQueryable into a string of SQL?


eg.

var result = myObject.Where(x => x.prop == 5);

string s = result.toSQL();

Result:

s is "SELECT * FROM [myObjects] WHERE prop = 5"


Solution

  • If it's IQueryable/ObjectQuery you can use ToTraceString. If it's IDbSet/DbSet you can use ToString directly.