Generate LINQ, SQL Queries With LINQPad

Excerpts from the MSDN about LINQ

Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.

In this article I am sharing how to generate a LINQ query with a very facilitative tool, LINQPad.

The most important fact about LINQPad is, it provides substantial functionality to play around with tables within a LINQPad editor.

So let's see how beneficial this is for .Net freaks.

A very initial look of LINQPad shown below:

Linq1.jpg

If you notice there is an option to create a connection with the desired database. I will use the Northwind database for my demonstration purpose, here are a few sequential steps to establish a connection.

Click on "Add connection"; a window will appear.

Linq2.jpg

Choose "Default (LINQ to SQL)" and click on the "Next" button. A new window will appear, fill in the required details to get connected with the desired database. Since I've chosen the default provider, "(local)" Server, the SQL Authentication option is used and the desired credentials are passed.

Linq3.jpg

Click on the Test button and a dialog box will appear with the "Connection Successful" message as depicted below:

Linq4.jpg

Click on the "Ok" button and see the following image:

Linq5.jpg

To move ahead after the successful connection, now to run some query and see the benefits of this.

Before running a query in the LINQPad editor, please have a look at the toolbar section and hover the mouse over at least one (toolbar name is very easy to understand).

Linq6.jpg

As you can see in the window above, the connection is still pointing to none, though the connection has been made already, but the open editor is not pointing to any existing connection. Click on the connection dropdown and select something; mine is Northwind.

Linq7.jpg

You can also write your query in many modes that .Net supports. See the following image.

Linq8.jpg

I will run an inner join query here using the Orders and OrderDetails tables of the Northwind database.

At the very first time I will choose the language SQL and run the following SQL query with the green button.

select od.Quantity ,o.OrderID from dbo.Orders as o inner join dbo.OrderDetails as od
on o.OrderID = od.OrderID

Output:

Linq9.jpg

Now I run the following LINQ query in the LINQPad editor and choose the Language option as C# Statement(s).

var Result = from o in Orders join od in OrderDetails on o.OrderID equals od.OrderID
                           select new  { od.Quantity,o.OrderID};

I run the preceding query but nothing is produced. The reason for that is we need to use an extension method Dump() that is a built-in method of LINQPad to print the results.

Let's run the preceding query again with the Dump method.

var Result = from o in Orders join od in OrderDetails on o.OrderID equals od.OrderID
                          
select new  { od.Quantity,o.OrderID};
                          
Result.Dump();

See the output:

Linq10.jpg

There are also some builtin extension methods, right-click on any table and see the answer as in the following image.

Linq11.jpg

You can download LINQPad from here http://www.linqpad.net/.

So far so good.

Hoe you enjoyed this demonstration.

Keep coding and Be Happy.

Up Next
    Ebook Download
    View all
    Learn
    View all