(Also see LINQPad Tips and Tricks - Part 2)
Whilst pretty much all .NET developers have heard of LINQPad - from my experience, a much smaller percentage have actually given it a proper go and use it on a regular basis. Up until a year ago, this also included myself. I'd heard of it, but never properly used it, or appreciated its true potential. Which is a massive shame, as after just one day, I immediately bought the fully licensed version and have been happily using it daily ever since! I now very rarely feel the need to start up SQL Server Management Studio.
Coincidentally at my current contract which I only started just over a month ago - pretty much everyone there also uses it quite heavily. With a lot of their scripts being LINQPad scripts!
The name suggests that it's just about LINQ, which whilst awesome in itself, is only a very small subset of what LINQPad is all about. You can write any .NET code in it - it doesn't necessarily have to be querying a database. You can also save them as .linq files, and run them from the command line via the
The query output in LINQPad is insanely powerful. It doesn't just display a grid like in SSMS, but also allows you to drill down into complex properties on multiple nested levels. When the output is of a query against a relational database, then these complex properties relate to foreign key references. So instead of resorting to writing complex join queries, you can either include the navigation property in your query, or just click on the hyperlink in the result window to drill down further.
Below is a screenshot of the output window using the famous
Northwind demo database ...
Since I've started using LINQPad, I've found quite a few little non-obvious tips, that I thought worth sharing. If there are any tips that you have that aren't here, then please share in the comment section below! :)
If you're only interested in a single result, then by using one of LINQ's singular methods -
SingleOrDefault - the output is transposed so that each field is a row rather than a column, making it much more readable.
As an example, compare the following two screenshots ...
Much more readable when you only need to see a single record. Plus, no horizontal scrollbar required!
To quickly apply a network connection to a query window, you can simply just drag and drop it from the left-hand connection pane onto the query window.
As a bonus, if you hold down the
ctrl key whilst doing this, you can drag multiple connections to the same query window, and reference each from your query!
LINQPad comes with an executable called
lprun. This can be used to run LP scripts either from the command line or any automated processes, eg. your build system.
LINQPad also supports Nuget packages. So you can add packages to your scripts, and use them just as you would in a normal .NET application.
The power that adding nuget packages gives you shouldn't be underestimated. Given the huge number of packages in Nuget - this opens so much more potential for your scripts - eg. generating charts, or using libraries like JSON.NET, etc.
Whilst I would highly recommend purchasing LINQPad, which adds debugging support - if you do need to debug a script from Visual Studio - you can call
Debugger.Launch() from your LP script to allow you to attach the Visual Studio debugger. But don't actually do this - just buy the full version and support the developer! :)
There's a built-in static class called
Util which has a number of useful methods. Typing
Util. will give you intellisense listing the available methods, but here are a few notable ones ...
Plus many more. The methods include XML comments, so the intellisense will give you a description of what each method does.
There are quite a few shortcuts, and they can be viewed from the
Help -> Keyboard/Mouse Shortcuts menu option. Ignoring the obvious
F5 to run your current query - there are a few less obvious hotkeys that I find extremely useful, so thought I'd explicitly pick them out ...
Languagedropdown - eg.
C# Program, etc.
If your query has a current database connection, then you can just call
SubmitChanges() to save any changes that you've made to your current database context.
If your code or a dependency requires something that would normally be in an
web.config, then if you choose the
Query -> App Config menu option - you'll be able to either copy the contents of a config file into the dialog, or even reference an existing one.
If you want to render a clickable hyperlink, then you can use:
new Hyperlinq ("www.danclarke.com", "My Blog")
It even has an overload with an action delegate allowing you to run code when the link is clicked on!
new Hyperlinq (() => MessageBox.Show("Dada!"), "Click me")
If you need to insert a placeholder into the results window in which your script will populate later on, you can use a Dump Container. Below is an example demonstrating how this works:
var dc = new DumpContainer(); "Before".Dump(); dc.Dump(); "After".Dump(); dc.Content = "Some content to insert";
If you're using a library which writes a lot of output logs, and you'd rather not show this in your LINQPad output, then you can use the following command to mute debug output ...
What development application would be complete without a dark mode! In the preferences dialog, there's an option to choose a dark theme :)
So hopefully if you don't currently use LINQPad, this post has at least motivated you to download it and give it a try - you most certainly won't regret it! If you are already a LINQPad user, then I hope there's been at least one of the tips that have helped - please also comment below with any of your own favourite LINQPad tips! :)