score:10

Accepted answer

the commercial tools orm profiler, entity framework profiler or hugati query profiler will both give you a stack trace for the methods which generated the sql. that makes it fairly easy to find the linq in code, though it isn't displayed directly.

these tools also have the advantage that they make it easy to find inefficient queries amongst the many other sql statements executed by the app.

score:0

i don't think you can modify the generated sql easily but what you can do is to get the generated sql before sending the query to the database.

so you can log every query in a separate textfile with timestamp and source code context information. but that means to modify each place in your source where linq queries are sent to the database. maybe there is an extension point somewhere in the datacontext class for simplifying this.

anyway here is the code to get the corresponding sql query for a linq query:

yourdatacontext dc = new yourdatacontext();
iqueryable<yourentityclass> query =
    from e in dc.yourentities
    where ...
    select e;

string command = dc.getcommand(query).commandtext;

score:2

i bet entity framework profiler (http://efprof.com/) would help you out. the workflow is very different from what you asked for (which would be pretty cool btw). it is a good tool, and is worth a look even if it's not your final solution.

good luck!

score:2

if you have access to the asp.net code where the linq code is you can more or less know which query you are looking for, copy it into a freeware tool called linqpad and run it directly there to get the generated sql statements. http://www.linqpad.net/

you need first get the linq queries on your .net code, create a connection to your datasource, paste the linq code in new queries and run them. you will get the sql query generated from the linq code.

for example:

from e in etusers
where  e.loginname.contains("a")
orderby e.loginname
select e

sql results tab:

-- region parameters
declare @p0 varchar(1000) = '%a%'
-- endregion
select [t0].[userid], [t0].[usrfirstname], [t0].[usrlastname], [t0].[loginname], [t0].[location], [t0].[password], [t0].[usremail], ...
from [etuser] as [t0]
where [t0].[loginname] like @p0
order by [t0].[loginname]

this is probably not exactly what you are looking for, but it is worth knowing about this tool since it is very helpful to quickly test linq queries. there you can quickly edit and run to improve the code without recompiling the whole stuff.

score:3

although it is not a free tool, this may provide the information you need:

http://efprof.com/

there is also a less expensive tool described here, which i have not used, but it looks very promising:

http://huagati.blogspot.com/2010/06/entity-framework-support-in-huagati.html

http://www.huagati.com/l2sprofiler/


Related Query

More Query from same tag