It is very common that may come in your mind that, how we can see SQL query being generated at the back end for LINQ query.

Assume you have a LINQ as below,

image

If you want to view SQL query generated for above LINQ on console screen you need to add just one line of code as below,

image

If you closely examine log it is a property in DataContext class and it is of type textwriter.

image

Since it is of type text writer you can save it to a text file as well. If you want to log you need to follow below steps.

Step1

Create a class overriding System.IO.TextWriter class.

image

Step 2

Configure log file in configuration

image

Step 3

Log the query as below

image

Full source code of above explanation is given below ,


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Relatedtable
{
    class Program
    {

        static DataClasses1DataContext context;
        static void Main(string[] args)
        {
            context = new DataClasses1DataContext();
            context.Log = Console.Out;
            context.Persons.InsertOnSubmit(
                            new Person
                            {
                                FirstName = "The ",
                                LastName = "Poet",
                                HireDate = DateTime.Now,
                                OfficeAssignment = new OfficeAssignment
                                {
                                    Location = "Jamshedpur"
                                }

                            }
                            );
            context.Log = new LogLINQSQL();
            context.SubmitChanges();
            context.Log = Console.Out;
            Console.ReadKey(true);

var result = from r in context.Persons
                         where r.FirstName=="Dhananjay"
                         select r ;
            context.Log = Console.Out;
            foreach (var r in result)
            {
                Console.WriteLine(r.FirstName);
            }
            Console.ReadKey(true);

  }

 }

    class LogLINQSQL : System.IO.TextWriter
    {
        public override void Write(char[] buffer, int index, int count)
        {
            System.Diagnostics.Debug.Write(new String(buffer, index, count));
        }

        public override void Write(string value)
        {
            System.Diagnostics.Debug.Write(value);
        }

        public override Encoding Encoding
        {
            get { return System.Text.Encoding.UTF8; }
        }
    }
}

I hope this post was useful. Thanks for reading  Smile

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

Advertisements

One thought on “Logging in LINQ to SQL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s