Display the T-SQL syntax of your LINQ query

To display the T-SQL query or command for your LINQ expressions in Linq to SQL and the Entity Framework:

using (DataContext db = new DataContext()) //Linq to SQL
{
    var query =
        from p in db.Products
        select p;

    string tsql_linq2sql = db.GetCommand(query).CommandText;
    
    Console.WriteLine(tsql_linq2sql);
}

Console.WriteLine(t_sql);

using (ObjectContext db = new ObjectContext()) //Entity Framework
{
    var query =
        from p in db.Products
        select p;

    string tsql_ef = ((System.Data.Objects.ObjectQuery)query).ToTraceString();
    
    Console.WriteLine(tsql_ef);
}
Advertisements

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