Sparky's Tool Tips: LINQPad

sparky

Brian Schroer

Posted on August 10, 2022

Sparky's Tool Tips: LINQPad

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, 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

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

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, if you query a table with foreign keys, LINQPad shows those relationships as navigation links to those entities:
Navigation

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

LINQPad SQL query

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

Generated SQL

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"

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

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 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/.

💖 💪 🙅 🚩
sparky
Brian Schroer

Posted on August 10, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Sparky's Tool Tips: LINQPad
csharp Sparky's Tool Tips: LINQPad

August 10, 2022