Click here to Skip to main content
13,139,132 members (63,462 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 1 Dec 2011

Log your LINQ query

, 1 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Log your LINQ query

Most of the beginner developers who are using LINQ to SQL as their back-end to talk with the database (i.e. to perform the database CRUD operation) don't have an idea what query gets fired to database out of LINQ query.

Lastly, I asked to log the query that fires to database out of my LINQ query. So as a solution, I found one makes use of SQL Server Profiler to check fire query. But with the profiler, I am not able to log the queries.

I found one solution is to make use of Log property of DataContext object. Log property allows me to log the queries in the file. Consider the below code:

//created temp file 
(System.IO.StreamWriter sw = new System.IO.StreamWriter(@"e:\tempdatacontext.log"))
    EmployeeDataContext edb = new EmployeeDataContext();
    //assigned streamwriter to the log property of datacontext
    edb.Log = sw;
    var cust = from c in edb.Customers
              join d in edb.Distributors on
                new { CityID = c.CityId, StateID = c.StateId, 
                      CountryID = c.CountryId, Id = c.DistributorId }
                new { CityID = d.CityId, StateID = d.StateId, 
                      CountryID = d.CountryId, Id = d.DistributorId }
              select c;

    List<customer> custList = cust.ToList();

So once the code gets executed, it's time to check the temp file. As I opened up the file, I found the following query gets fired on my database:


It's fun to find the query gets fire to database and you get to know if there is any problem in the LINQ query you wrote.


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


About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog

StackOverFlow -
My CV :-


You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Kanasz Robert1-Dec-11 2:40
memberKanasz Robert1-Dec-11 2:40 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 1 Dec 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid