Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Monday, 19 November 2012

LINQ : LINQPad

 LINQPad lets you interactively query databases in a modern query language: LINQ.  Kiss goodbye to SQL Management Studio
LINQPad supports everything in C# 4.0/5.0 and Framework 4:
And that's not all - there's also specific support for:
And LINQPad is more than a LINQ tool: it's an ergonomic C#/VB/F# scratchpad that instantly executes any expression, statement block or program with rich output formatting – the ultimate in dynamic development. Put an end to those hundreds of Visual Studio Console projects cluttering your source folder

LINQPad Screenshot

Source : http://www.linqpad.net/

Linq : Gettng T-Sql Squery With ToTraceString() method

We can use ObjectQuery.ToTraceString() to figure out what is going on behind the scenes. ToTraceString() returns the TSQL (assuming you are using the SQL Server provider) for that ObjectQuery.

Since we can see the TSQL code that EF generates I can poke around and figure what is going around behind the curtain.

For example if you ran this in LINQPad

void Main()
{
    PubsModel.pubsEntities context =
new
PubsModel.pubsEntities(EF_CONNECTION_STRING);

   
// Entity SQL:
    // Returns ObjectQuery<T> directly
   
ObjectQuery<author> esqlq = context.CreateQuery<author>(
       
"SELECT VALUE a FROM authors AS a"
);
    esqlq.ToTraceString().Dump(
"--CreateQuery"
);
   
   
// Linq to Entities
    // ObjectQuery<T> implements IQueryable<T>
    // This statement returns an ObjectQuery<T>
    // casted as IQueryable<T>
   
IQueryable<author> linqq = from a in context.authors select
a;   
   
// So it must be cast back to execute ToTraceString()
   
((ObjectQuery)linqq).ToTraceString().Dump("--Linq Query"
);
}


You would get:

--CreateQuerySELECT [Extent1].[au_id] AS [au_id], [Extent1].[au_lname] AS [au_lname], [Extent1].[au_fname] AS [au_fname], [Extent1].[phone] AS [phone], [Extent1].[address] AS [address], [Extent1].[city] AS [city], [Extent1].[state] AS [state], [Extent1].[zip] AS [zip], [Extent1].[contract] AS [contract]
FROM [dbo].[authors] AS [Extent1] 
--Linq QuerySELECT [Extent1].[au_id] AS [au_id], [Extent1].[au_lname] AS [au_lname], [Extent1].[au_fname] AS [au_fname], [Extent1].[phone] AS [phone], [Extent1].[address] AS [address], [Extent1].[city] AS [city], [Extent1].[state] AS [state], [Extent1].[zip] AS [zip], [Extent1].[contract] AS [contract]
FROM [dbo].[authors] AS [Extent1]