Linqpad – the virtual scratchpad you never knew you wanted (until now)

Having production bugs named after me since 2012. Loving clean architecture, pinnipeds and putting stuff in parentheses. I do compete in finswimming and underwater orienteering. Feel free to join me. 😉

Many engineers keep a paper scratchpad on their desk to doodle ideas and concepts that can eventually be turned into a working piece of technology. But there is also a benefit in keeping a virtual scratchpad as well – and linqpad is just the right tool for the job. 

It’s all about the Dump() 

The single most powerful feature of linqpad is the Dump() extension method that you can call on any object. It will analyze the object graph and use its fine-tuned heuristics to present the object’s data in the most efficient way possible. It can also render bitmaps, xml data, and even WPF and WinForms controls! 

Boosting your productivity game with linqpad 

There are two main ways to leverage linqpad for more productivity:  1) scripting in your favorite language (C#/F#/VB.NET) and 2) easy database querying, edits, and data analytics. 

Let’s look at each one in more detail. 

Scripting with linqpad 

Once you start using linqpad, gone are the days when you had to tediously set up yet another console project when you wanted to quickly whip up a simple CLI-based tool (yes, since C# 9 you can use Top-level statements for the same reason, but it lacks many of the possibilities offered by linqpad). 

Depending on the desired use case, linqpad script can be run in three different modes:   

  1. Expression:
  • The script can contain only a single expression.  
  • No need to add ; at the end and Dump() is called automatically on the expression result.   
  • Great for quick DB queries, generating guids, and other single line shenanigans.  
  1. Statements:
  • The script can contain multiple expressions.  
  • Dump() has to be called explicitly to display expression result.  
  1. Program:
  • The script will contain a Main() method serving as an entry point (think of this mode as a Program.cs class equivalent.).  
  • Other methods, classes, structs, and records can be defined in the script in this mode.  
  • Dump() has to be called explicitly to display expression result. 

Linqpad also comes with a built-in nuget package manager to leverage any package from the amazing .NET ecosystem and integrated VS-style debugger (that one is available only in the paid version though).

You can swim and you can code with Jiří!

And you can talk linqpad too if this post hasn’t said it all…

Once your idea becomes polished enough, you can simply copy-paste the code into your existing project in your main IDE (e.g. Visual Studio or Rider) or save it to a .linq file and share it with your colleagues. The file can then be run via linqpad or as a process in your favorite shell via the lprun command (you can access STDIN, STDOUT, and command arguments in your linqpad script the same way you would in a typical .NET console application). It’s as simple as this: 

data.csv > lprun myquery.linq CustomArg1 CustomArg2 > output.txt 

Data analytics with linqpad 

Typical DB workflow with linqpad starts with adding a new DB connection. Based on the connection, a LinqToSql DB context is generated. This context is then used for querying of the database in the same manner as it would be used in any .NET application. Another option is to load a typed Entity Framework DataContext from an existing dll. 

There are a myriad of other officially supported DB drivers (Oracle, PostgreSQL, mysql, MariaDB, Azure table storage, MongoDB, SAP HANA, and more). This makes linqpad a mighty tool when working with a database with an unfamiliar query syntax. For me, it’s a life saver when I’m forced to work with oracle databases (Oracle SQL has a very specific syntax and most of MSSQL/MySQL developers find it quite irritating. By using linqpad you can avoid touching Oracle SQL altogether.). 

Querying using LINQ is not only more convenient but also almost friction-free because of a rock-solid intellisense thanks to the typed data context. Most SQL IDEs offer at least some sort of intellisense, but it pales in comparison to the linqpad one. 

Navigation properties 

When examining the query results there might be a need to explore the related entities. All you need to do is click the desired navigation property of the entity and all related data will be automatically loaded and displayed. No need for an additional query. 

Navigation properties

Results editing 

There is nothing more annoying than writing a lengthy INSERT/UPDATE statement just to add a single row or to edit a single value in a table. To save you the pain of doing so, linqpad allows you to switch to a grid view and make the necessary edits there. 

Results editing

Results export 

Once satisfied with the query results, there is an option to export them to either Word, Excel, or HTML. Another export option is using the bult-in Util.WriteCsv() method to write the result data directly into a csv file. 

Other handy, not-so-known features

There are a myriad of smaller, built-in features of linqpad that you probably won’t use every time, for example: 

Password input 

For obvious security reasons, storing passwords in code is a bad idea. But by using the Util.GetPassword() method, passwords can be entered in a safe manner. 

Password input

Interactive regex evaluator 

Writing and debugging complex regular expressions is always a pain. Linqpad comes in with a handy tool to ease this tedious task. And if your memory is as weak as the author’s, you will definitely appreciate the built-in regex reference and a cookbook with the most commonly used regexes. 

Interactive regex evaluator

Progress bar 

Query execution progress can be displayed in the linqpad’s progress bar or for each execution step in the results pane. 

Progress bar

CSV exports 

The query results can be easily exported in CSV format either by Util.ToCsvString() or Util.WriteCsv() method. 

CSV exports

Charting 

It is possible to visually represent the query data via built-in charting. Just call the Util.Chart() method. 

Charting

Diffing 

Comparing data in two objects (to any depth) can be achieved using the Util.Dif() method. 

Diffing

Caching between runs 

To avoid running expensive queries multiple times, the result can be cached between query runs using the Util.Cache() method. 

Caching data between runs

Final thoughts & resources

After learning about all the possible ways to utilize linqpad (doodling/scripting/data analysis), you’ll be surprised how quickly it will become an essential sidekick, having your back every day of your programming journey. 

© Featured image by Kelly Sikkema from Unsplash

Having production bugs named after me since 2012. Loving clean architecture, pinnipeds and putting stuff in parentheses. I do compete in finswimming and underwater orienteering. Feel free to join me. 😉
Share:
Share on twitter
Share on facebook
Share on linkedin

More About

Mews Systems Copyright ©
Website by

Cookie Management

Here you can manage your preferences regarding cookies:

Essential cookies

Always active
Essential cookies enable core functionalities of the website such as marking your data inputs, network management and accessibility.

Analytical cookies

Analytical cookies help us improve our website by collecting and reporting information on how you use it. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

Advertising cookies

Advertising cookies for delivering tailored and customized advertising.