Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

LINQPad, A Developer’s Best Friend

4.83/5 (5 votes)
13 Nov 2018CPOL14 min read 14.2K  
LINQPad - a developer’s best friend

Introduction

In this post, I discuss one tool that is indispensable from my toolbox (also by popular demand :D), from testing out ideas, to developing custom controls, I default to LINQPad most of the time, and not for just “development” topics.

Warning, a wall of text is incoming 😀 .

We are going to go through what it is, and a few examples of what you can do with it.

So What is LINQPad?

So LINQPad might sound like a tool that is only for LINQ, but it’s so much more.

It is free to use, though the free edition has quite a few missing features that make your life so much more comfortable, that I think it’s worth the license price, especially the ability to debug with the Premium license.  Do keep in mind that except the usability features, you don’t have to get a license if you have no use for “Intellisense” like features, Nuget Packages, debugging and cross Database queries (yes, you can have a query that uses multiple databases). For a list of the full features a license gives you, you can look here.

Coding Technologies

Now for the fun part, you can write, compile and run any code from the following technologies (and different modes explained in line):

  • C#
    • Expression – allows you to write a single expression to evaluate. Expressions come in handy for database lookups or if you just want to run a snippet of code to see what happens, like for example getting all the running processes, reading a file, convert some DateTime to a different time-zone, do a computation, etc. Also in this mode, the expression doesn’t need to end in a semi-colon, but in the latest version, if you do put a semi-colon at the end of an expression, it changes the mode to the Statement, and this is configurable.
    • Statement(s) – this allows you to write and run multiple lines of code. Imagine you’re writing all those lines inside a method, this is where I spend most of the time when I use LINQPad because most “scripts” are just some set of instructions that do not need any complicated structure.
    • Program – this will give you a “Main” method in which you put in your code, and outside its scope, you can define custom classes and methods to experiment and play. Program mode is excellent when you want to design a class hierarchy, work with some event-driven API, design some custom controls (for WebForms or WPF, both work), and anything else you could do inside a typical class project. The downside to this is that if you make a very complex structure, it will have to be in one file (there are ways around that like compiling those closes into an assembly and referencing them and such) so it will get cluttered.
    • Side note: If you are in Statement(s) or Program mode and select a part of the code and hit “Run”, then it will run only the selected portion as an Expression, just like SQL Server Manager would do with queries if you’re familiar with that.
  • VB.NET
    • Expression – Same as above
    • Statement(s) – Same as above
    • Program – Same as above
  • SQL – This allows you to write raw SQL statements against a database, so in essence, you have a more lightweight IDE for SQL than SQL Server Manager.
  • ESQL – I have never used this mode, but from what I understand, it’s a way of having SQL work with custom types and application logic, if nothing else, at least it’s an option 🙂
  • F#
    • Expression – Same as above
    • Program – Same as above

An additional mention is that the latest LINQPad works with the latest version of .NET. That includes all its goodies like string interpolation, tuples, asyncs, local functions and more.

Data Context Connections

LINQPad supports a wide array of Data Contexts. The reason I’m not calling them databases is because besides the fact that it can connect out of the box to MS SQL and Azure SQL, there are quite a few other ways to connect to data providers, some out of the box (like support for WCF Data Services and Entity Framework) and some created by the community (like treating CSV and JSON files as data stores and NoSQL databases like Raven and Mongo).

Best yet is the fact that you can create your Data Context and then just install it into LINQPad if you can’t find the provider you need (not going to go into the details of how to create one, there are plenty of examples on Github about it).

Queries and Samples

Another significant fact about LINQPad is that everything saves into a particular file format (basically a text file with an XML header) which allows you to save the code you have written and re-used it afterward. If I were to save every script I wrote in LINQPad into console projects, I would go crazy, and if you’re not convinced, try doing a search for LINQPad on Github, and you will see pages upon pages of repositories just dedicated to these scripts and nothing else.

And by default, you get a query called “My Extensions” in which you can save your methods, classes and extension methods that you want to reuse across your regular queries, basically, a file that all queries reference.

One last point before we move onto samples, you can go to any open query, open the files menu and click Upload to Instant Share. Clicking this will upload your script and give you an URL that you can share with others if you want to send it to them like this. A word of caution here, the URL is public and can only be reverted from that URL as far as I know, so be mindful of what you are sharing. To open a received URL, you can just say click “Open” under the file menu and paste in the URL or just do it in the browser which will download the script to your computer.

As for Samples, well these are scripts that have already been written for you as examples, and you can download more for free. What I usually do is that I download all of them, and then I can search in them, like for example if I need to remember a particular syntax for XML or HttpClient or a ton of other stuff, I can just use the search function, find it, run it, then tinker and play with it to adapt it to my scenario.

One of these samples folders is even a tutorial as to how to use LINQPad. But as if that wasn’t enough about samples, there are Nuget package authors that embed samples into their libraries, so if you have a licence that allows for Nuget (from Developer licence upwards), then you can actually search Nuget and the libraries that contain samples are accompanied by a text letting you know which packages provide samples so that you can play with a library without even having to write the code for it.

And here’s a bonus. When I write queries or scripts, I tend to reach a point that it kinda does what I want but I don’t want to mess it up, and also I want to further experiment with it, so you can right-click on any query and clone it and it will create a whole new instance of everything just as it was but independent of the first one, so you can tweak and play with your scripts in many ways before finally saying you want to save them.

The Outputs

When running a script, you can choose one of 2 default modes:

  • Rich Text
    • In this mode, everything outputs in a frame that wraps the output inside HTML so that you can even customize what and how it shows.
    • When working with data that has relationships like foreign keys inside a database, the connections will be highlighted as links so you can explore the data. You can’t imagine how helpful this can be instead of continually writing join clauses to look for that one item you are interested in.
    • It has a limit as to how much it can output
    • Can be customized with different visualizers
    • Runs a little slower with vast amounts of interconnected data
  • Data Grid
    • Is faster than Rich Text due to the fact that it doesn’t format or load as much data
    • If the provider you are using (like the default one for SQL) allows for editing and manipulating data, then you can run a query against a table and then edit the data by hand just like how you would “Edit rows” in SQL Server Manager
    • Allows for unlimited data (if you have the RAM for it)
    • Every relationship link opens a new data grid so you can breadcrumb through them.

Besides the result outputs we saw earlier, there are a few additional outputs as well (these can be extended as well like for example there is a visualizer for Query Execution Plan). These outputs are the following:

  • The lambda symbol (λ) – This is useful when writing comprehensive LINQ like from x in y where x > 2 select x, this will show you which LINQ method calls are used
  • SQL – This show’s you what SQL statements were run if you run your query against a SQL database, and when clicking on a relationship link in the output, this panel gets updated with what ran in the Database, another use for this is to write a LINQ statement and then see how the provider converted it to SQL in case you’re like me in which you know how to write something in LINQ but it’s a bit akward to write it in SQL
  • IL – This is for people that want to see what their code actually compiles into. I have to admit that I rarely used this one but it’s good to have if you have to do some IL work (hint as to what works well with this, is IL Weaving in which you change a DLL after it’s compiled)
  • Tree – This shows you the syntax tree of the written code using Roslyn, the .NET compiler. Again, I didn’t have the oportunity to benefit a lot from this, but as a usability, I can see pasting code in LINQPad to inspect it and then maybe write a custom Analyzer based on Roslyn using this syntax tree, like enforcing team rules inside a project.
  • Additional non-default ones – more visualizers show up here either when making extensions or when creating UI components for WPF or WinForms, like buttons, lists and any such control you might be working on.

And the last item on the list of features for the outputs is the Export drop-down that hosts a list of buttons that lets you export the results to be used in other ways, like Excel if you want to embed some results into a spreadsheet, or Word documents or HTML.

The API

Well, we talked quite a bit about what the app itself can do, but the app also has its own API (which can also be extended via plugins) that make it all the better.

Dump()

One of the best extension methods I have ever encountered so far is the .Dump() extension method, I even wrote a few rudimentary ones myself when needed, inspired by the Dump method found in LINQPad. What this extension method does, it receives any item, outputs it and returns it like nothing ever happened, here’s an example of that:

C#
Client
.Dump()
.Where(a=> a.PhoneNumber != null)
.Dump()
.Select(a=> a.UserName)
.Dump()
.OrderBy(a=> a)
.Dump()

Using this simple idea of getting an item, any item, and returning it as it is allows you to plug this method call anywhere and see what happened along the way, which makes debugging so much easier. For this example, I would have the whole collections of clients, the ones who have a phone number, the collection of names and the collection ordered.

Keep in mind that I said any item earlier, that means you can also call it on complex objects and collections to see what’s inside them, their properties, their public fields.

P.S.: This extension method is also called by default when running in Expression mode.

Also, it is no wonder that this method also has a set of overloads and knows how to interpret the object that it receives, that’s why I called my implementation rudimentary, even though they did what was needed at the time.

Util.GetPassword

A lot of other neat functionalities can be found under the Util class, these are a set of functions that change how the application behaves, one very important one is the GetPassword.

Here’s an example of it, say you want to make an email client  to sort your email, search for keywords and such (I needed it at one point 😀 ), but you don’t want to hard-code your secret passwords in a script you might share or upload to a public site one day. So in LINQPad under the File menu, there is something called Password Manager, so you can store sensitive information in a key-value pair format and use the passwords in your scripts without actually needing to save them in clear text. These passwords are only saved on the local machine and encrypted and the way to access them in your scripts is by making a call to Util.GetPassword(“key”) where you replace the “key” with the one you used in the manager.

Other API Magic

Though I won’t go into detail about the API, the tutorial does a great job of it as it is, I did call out the two most important ones, though for a more comprehensive list besides the tutorial, you can look at this article on StackOverflow.

Last Great Feature

When you download LINQPad, it comes with an additional application called LPRun, if you want to keep your scripts as they are in their .linq format, then you can use this executable to run such a script without opening up LINQPad, this is great for lightweight scripting that needs to run on a server based on a timer or such, all you need to do is call that executable and pass in the full path of a script you want to run.

Real Examples

I just want to enumerate just a few of the things I did in LINQPad and hopefully spark some new ideas for you as well, this is by no means a complete list since this tool makes it so easy to sketch out new ones.

  • A countdown timer for an absolute point in time (hey, it helped with what I needed)
  • A script that clears out my Firefox history selectively
  • A web crawler
  • A script that look through files for keywords, that can be configured for number of threads and custom logic (we will see an example of this very soon in the next post)
  • A random string generator (for when you’re out of ideas for a string in sample data or for a password, configuring the length and the allowed characters)
  • A contact list for my friends with real-time timezone information so that I never call them too late or early
  • A script to synchronize two directories based exactly on what I needed at the time and the speed I wanted.
  • A WinForms implementation that charts the data from the database so that I can just export it and paste it into a document
  • An email parser, for tracking down custom rules for emails
  • An encryption/decryption script for those sensitive documents and files
  • A script that computes my work timesheet (back when I was filling those out 😀 )
  • A currency exchange tracker

These are just some ideas that I needed at one point or another but never needed to make full applications out of them, the advantage is that LINQPad is very lightweight and opens fast, and if I ever need to tweak one of those, I can just do it without needing to recompile and test and such and I can do it with very fast feedback.

Conclusion

I know this post was quite extensive, but this tool is quite versatile, personally I have it on every machine I work on, and it makes life so much easier, from testing out the code that gets posted here (except when we do solutions with unit tests and such), to finding out what week day will be 19 days from now (yes, I might be too lazy to get a calendar and do the math), to even referencing assemblies from legacy applications to test a functionality that I’m working on without having to do 15 clicks to get there just to confirm the design.

Like stated above, the next post is not dependant on LINQPad (few things rarely are), but it sure helped out a lot.

I hope you enjoyed it and see you next time.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)