LINQPad Making My Life Easier

Every once in a while, you encounter a development tool that dramatically impacts your programming life.  Recently, I started using LINQPad, and it has done just that.

I started using this tool on a recent consulting job.  The client’s product was experiencing some really big performance issues on the SQL Server.  They used LINQ to SQL for all of the data access.  The problem frequently turned out to be malformed LINQ queries, queries developed without any clue what the generated SQL was doing (or even looked like for that matter).  Enter LINQPad.

With LINQPad, not only can you run LINQ to SQL queries, you can run SQL queries, and you can even run code snippets.

You start by adding a new connection.

Add A Connection For LINQPad

And, setting the properties accordingly.

LINQPad Connection Properties Dialog

One of your options here will be to pick a Custom LINQ to SQL Data Context.

LINQPad Pick DataContext

This will allow you to browse to the assembly for your existing project’s data context.   And, it will enable you to run the same queries that you are using in your data access layer.  You can revisit your connection settings later by right-clicking on the connection and choosing Properties.

LINQPad Connection Properties

Once you have your connection setup, you can select it as your database and start analyzing your queries.

LINQPad Database Selection

But, there are still a few settings that I regularly use.  First, I set the language to C# Statement(s).  This is used for running multiple queries/statements, whereas C# Expression is used for a single query (and doesn’t require semi-colons).  You can also use C# Program if you want your test to contain custom classes and methods.

LINQPad Language Selection

After selecting the appropriate language, I’ll add the namespace references that are necessary to complete the testing.  And, if you have commonly selected references, you can set them to be included by default.  One that I commonly use is System.Transactions.

Right click in the query window and choose Advanced Query Properties.

LINQPad Query Properties

Then choose Add… and browse to the assembly that you need to reference.

LINQPad Query Properties Dialog

Next, flip to the Additional Namespace Imports tab and enter the namespaces that you need to import from the assembly you have referenced.

LINQPad Using Namespace

You can now make use of the assembly, similar to the way I made use of TransactionScope below.

LINQPad TransactionScope Using Clause

 At this point, you should be able to really take advantage of all that LINQPad has to offer.  Processing queries, verifying the SQL generated, and evaluating the results is quite straightfoward.

Enter a query and choose Play.

LINQPad Test Query

Click SQL to see the query that is generated as a result.

LINQPad SQL Dump

Using the Dump extension method appropriately in the query will allow you see the results in the Results tab.

LINQPad Results

And, keep in mind that this tool is useful for way more than just analyzing LINQ, LINQ to SQL, and SQL.  It is a full blown snippet compiler, as evidenced by the sample below.

LINQPad Snippet Results

Hopefully, I’ve shown you that LINQPad is great for a whole host of scenarios… testing a proof of concept, output formatting, regex testing, etc.  Even if you don’t work with LINQ or LINQ to SQL, do yourself a favor and download LINQPad today.

Rss Feed