Page 1 of 1

Capture SQL Generated from Linq

Posted: Fri 06 Feb 2015 23:07
by lhengen
Is there a way to capture the SQL generated from LINQ queries short of capturing traffic sent to the RDBMS? This would be useful for learning LINQ by experimentation.

Re: Capture SQL Generated from Linq

Posted: Mon 09 Feb 2015 12:14
by AlexP
hello,

Yes, you can retrieve the text of the last executed SQL query, that was generated according to the LINQ query. For this, you can execute the following code:

type
TDataContextUtil = class(TDataContext)
end;
.....
TDataContextUtil(EntityContext1).QueryCompiler.LastQuery.SQL;
.....

You can samples in the Demo application, in the 'Working with LINQ queries' branch.