Use LINQPad for more than LINQ

Posted by Filip Ekberg on 17 Sep 2012

I like to spend time on StackOverflow and contribute by answering as many questions as I have time to. Many of the questions consist of code that doesn't always work as expected. In these times I find that LINQPad is the perfect tool to use when you want to run the sample code or create smaller samples yourself for your answers.

Don't be confused by the name!

Just because the name is LINQPad, it doesn't mean it only does LINQ. Even if evaluating and running expressions is what it does best. LINQPad will allow you to run the following code types:

  • C# Expression
  • C# Statement(s)
  • C# Program
  • VB Expression
  • VB Statement(s)
  • VB Program
  • SQL
  • ESQL
  • F# Expression
  • F# Program

This makes LINQPad really powerful!

Best of all, there's a free alternative with a little less sugar on it. I recommend buying a license to support this amazing developer tool though and it will give you autocompletion in LINQPad, which makes it even more powerful!

Running C# code

Let us take a look at LINQPad and what it looks like. Here's what LINQPad looks like when you first start it up, it's clean and intuitive:

By default, it's preset to running C# Expressions, but you can easily switch between the different types of code snippets to execute in the dropdown menu:

In an expression, you can't declare variables, think of it like you can't add multiple statements. This is an example of a C# Expression:

new [] {11,20,33}.Where(x => x % 2 == 0)

We can execute this by pressing the green "run" button or by pressing F5 (as in Visual Studio). This will display a result of the expression as you would imagine any REPL would do:

Now let us assume that we have a more complex code snippet that we want to execute and we want to get information about variables along the way. LINQPad hooks in an extension to object which provides a method called Dump. This will dump information about the object that you use it on.

The more complex code snippet will be a BubbleSort implementation in C#. First we create a list of integers, order it randomly and then sort it using BubbleSort (side note: BubbleSort is the slowest sorting algorithm.).

To see that my items were actually sorted, I want to show the content of my list before and after it was sorted. To do this, I can use the extension method Dump() that comes with LINQPad. It will look like this:

This is the code that I executed:

var items = Enumerable.Range(0,4)
                      .OrderBy(x => Guid.NewGuid())
                      .ToArray();
items.Dump();

// Bubblesort: O(n^2)
bool done = false;
while(!done)
{
    done = true;
    for(int i = 0; i < items.Length - 1; i++)
    {
        if(items[i] > items[i + 1])
        {
            var temp = items[i];
            items[i] = items[i + 1];
            items[i + 1] = temp;
            done = false;
        }
    }
}

items.Dump();

This is a great example of how powerful multiple statements are in LINQPad. But there's more, you could also execute an entire program and have multiple classes and methods in it. But for larger projects like that, I personally like to create a Visual Studio project instead.

Notice the different outputs that we can select as well. If we wrote LINQ, we might want to see the actual SQL that is executed and if we write a couple of statements we want to see the IL:

LINQPad can do much more than this as well, we can have it connect to a database to run queries against our data. We can also change if the code is optimized or not; if we want to explore the IL that is generated when using optimization contra when it is not.

LINQPad is awesome and I use on a daily basis!

comments powered by Disqus