Sparky's Tool Tips: LINQPad
Tags: csharp, dotnet, productivity, tooling
Published: 2022-08-10 14:22:00

Of all my favorite tools, LINQPad is probably **_the_** favorite. I use it every day, several times per day.

##LINQ

In 2007, Microsoft's .NET 3.5 introduced [(L)anguage (In)tegrated (Q)uery](https://en.wikipedia.org/wiki/Language_Integrated_Query), one of the killer features of .NET, and Joseph Albahari introduced LINQPad almost immediately afterward, providing the ability to do database queries using LINQ as an alternative to SQL.

Here's a screenshot from an early version of LINQPad (it still pretty much looks the same):

![LINQPad version 1 screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/425cb53xrnsc09dmhi7i.png)

The screenshot above uses LINQ "query syntax", but of course, you can also use "method syntax" (.Where, .OrderBy). That's because LINQPad isn't just for LINQ and databases - It's for any any C#/F#/VB expression, statement block or program...

##Code "scratchpad"
If you want to quickly try out some code, instead of creating a console app in Visual Studio, you can just open up LINQPad and and type a line or two...

![LINQPad as a code scratchpad](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uz781jrcph7xmd71c1j5.png)

I use it a lot for quick code experimentation, and have amassed a collection of tiny utility "program" queries in LINQPad (JSON and XML formatting, removing line feeds and tabs from strings, etc.), but working with databases is LINQPad's main claim to fame...
 
##Database

As shown in the animation below that I "borrowed" from [Jiří Petržilka's blog](https://developers.mews.com/linqpad-the-virtual-scratchpad-you-never-knew-you-wanted/), if you query a table with foreign keys, LINQPad shows those relationships as navigation links to those entities:
![Navigation](https://developers.mews.com/wp-content/uploads/NavigationProperty-2.gif)

Querying with LINQ is what LINQPad was created for, but you can also query with plain old SQL:

![LINQPad SQL query](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/576d8txr25n7vwwkcm73.png)

If you do query with LINQ, you can click the results "SQL" tab to see the SQL that was generated:

![Generated SQL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5c6ksh8kzy82mhxyj59.png) 

I almost never use SQL Server Management Studio for SQL queries any more.

##Extensions, references & NuGet
You can reference .NET DLLs and NuGet packages and use them in your queries and create/save your own extension methods and classes and use them in your queries:

!["My Extensions"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tzix9hferp1pwexxcr3w.png) 

In this example query, the underlined code uses extensions I wrote to:
- run the SQL Server query "WITH (NOLOCK)" (in a TransactionScope with IsolationLevel.ReadUncommitted, actually) so it doesn't tie up the table
- convert from UTC time to my local time
- format a column containing JSON with indentation

![LINQPad query using extensions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k1rignh37d0bpvqjp58u.png)
 
##Pricing

There is a free version, which I'm ashamed to admit I used for probably thousands of hours before finally doing the right thing and sending some money Joseph's way.

There are [several premium license versions](https://www.linqpad.net/Purchase.aspx) starting at US $59, which add autocompletion & tooltips, code formatting, cross-database querying, etc.

##Summary

I'm afraid this post doesn't adequately convey LINQPad's awesomeness. I've been using it for years, and keep discovering new capabilities (interactive regular expression editing & testing !?). 

I urge you to give it a try.

- - -
You can learn more about LINQPad and download the installer from
https://www.linqpad.net/.