Click here to Skip to main content
15,880,392 members
Articles / Programming Languages / SQL
Tip/Trick

See the SQL Generated by LINQ

Rate me:
Please Sign up or sign in to vote.
4.98/5 (29 votes)
26 Feb 2010CPOL 61.1K   35   9
When using LINQ to SQL, it can be very useful to see the SQL commands that are generated by your LINQ expressions. Sometimes the results are surprising and you might be able to improve performance by tweaking the LINQ.All you have to do is set the Log property of the DataContext object. ...
When using LINQ to SQL, it can be very useful to see the SQL commands that are generated by your LINQ expressions. Sometimes the results are surprising and you might be able to improve performance by tweaking the LINQ.

All you have to do is set the Log property of the DataContext object. For example.

MyDataContext.Log = Console.Out;


This will cause the SQL commands generated by MyDataContext to appear in the output window in the debugger. You probably shouldn't leave this in your code without at least wrapping a #if DEBUG around it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Mark Lauritsen has been a software developer since 1983, starting at IBM and using a variety of languages including PL/1, Pascal, REXX, Ada, C/C++ and C#. Mark currently works at a midstream energy company developing Windows services and desktop applications in C#.

Comments and Discussions

 
GeneralMy vote of 1 Pin
fakeer muhammed10-Feb-13 4:39
fakeer muhammed10-Feb-13 4:39 
GeneralMy vote of 5 Pin
Carsten V2.021-Jun-12 8:57
Carsten V2.021-Jun-12 8:57 
GeneralReason for my vote of 5 Straight to the point and provides a... Pin
BrianBissell26-Jul-11 4:28
BrianBissell26-Jul-11 4:28 
Reason for my vote of 5
Straight to the point and provides alternatives...awesome.
GeneralReason for my vote of 5 Very simple and effective. Pin
Pravin Patil, Mumbai18-Jul-11 23:57
Pravin Patil, Mumbai18-Jul-11 23:57 
Generalnice and easy. Great! Pin
Member 414818817-Apr-11 19:39
Member 414818817-Apr-11 19:39 
GeneralReason for my vote of 5 Good to know Pin
Alomgir Miah A12-Apr-11 8:55
Alomgir Miah A12-Apr-11 8:55 
GeneralIs there anything like this for Entity Framework? Pin
Igor Merabishvili11-Apr-11 21:36
Igor Merabishvili11-Apr-11 21:36 
GeneralReason for my vote of 5 Thank you for the simple solution wh... Pin
linuxjr7-Apr-11 17:30
professionallinuxjr7-Apr-11 17:30 
GeneralReason for my vote of 5 This is so simple it makes me puke a... Pin
AspDotNetDev7-Apr-11 8:11
protectorAspDotNetDev7-Apr-11 8:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.