Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#
Tip/Trick

Bridging the Gap between Linqpad and Visual Studio (with JSON and Extension Methods)

Rate me:
Please Sign up or sign in to vote.
4.94/5 (16 votes)
8 May 2015CPOL5 min read 41K   154   27   5
This will allow you to integrate your linqpad code easily to Visual Studio

The download file includes both projects and the linqpad sample code.

Introduction

If you haven't heard about Linqpad, you should download it and give it a spin. It's a Notepad like application that lets you write your C# (VB, SQL, E# & F#) quickly, and compile and run it on the fly. It's really useful for work with Linq, but I often use it as a scrap pad whenever I need to quickly test an assumption or some code.

Don't get me wrong. I love VS, and when I need to debug some complex code  I'm the first to fire it up, start throwing break points and take advantage of all the bells and whistles it provides. But when I want to mock up an object or test a Linq expression, I'll usually pass on firing up VS (half a gig of memory, much longer loading time, need to create a new project, build it, compile it, you get the point), and do it in Linqpad.

One of my favorite features of linqpad is the .Dump() method. It let's you quickly dump to the screen whatever you're working with. Strings, Objects, Lists, you name it. As an extra bonus, it'll also return the object so you can use it as a continuation for temp outputs as well, for example:

C#
var list = Enumerable.Range(1,6)
                     .Dump("list of six ints")
                     .Where(x=> x%2 ==0)
                     .Dump("only the pair ones")
                     .Select (x => x*3)
                     .Dump("selecting the pair ones times three");

Will result in something like:

Image 1

Now sometimes, I'll get to the point where I need to bring in the big guns. It usually involves copying my code from linqpad into an existing solution, or creating a new one. It's not a big hassle, but I then need to manually delete all the .Dump() calls, or comment them out.

Let's see an example:

Enter the Dragon Genie (Linqpad)

In this tutorial, I'm using the following Genie class:

C#
class Genie{ 
    public Genie( int aYears     = 1000
                , int aWishCount = 3
                , string aColor  = "Blue"  )
    {
        YearsInTheBottle = aYears     ;
        WishesToGrant    = aWishCount ;
        Color            = aColor     ;
    }

    public int YearsInTheBottle { get; set; }
    public int WishesToGrant    { get; set; }
    public string Color         { get; set; }
    
    public void GrantWish(string aWish) {
        if ( WishesToGrant>0 ) 
        {
            Console.Out.WriteLine( "Your wish is my command." );
            Console.Out.WriteLine( "[{0}] granted.{1}"
                                 , aWish
                                 , Environment.NewLine    );
            WishesToGrant--;
            if (WishesToGrant==0) 
                Console.Out.WriteLine( "I'm finally freeeeeee...{0}"
                                     , Environment.NewLine);
        }
        else
            Console.Out.WriteLine("You're all out of wishes ...");
    }
}

If you've watched Aladdin, you should know what I'm talking about. If not, shame on you. Go watch it.

Now you can paste that code into Linqpad (select in the Language option "C# program") and do the following:

C#
void Main()
{
    var genie = new Genie().Dump();
    
    genie.GrantWish("I want to be smart");
    genie.GrantWish("I want to be a lumberjack");
    
    genie.Dump();
    
    genie.GrantWish("I want to be the king of Narnia");
    genie.GrantWish("I want to get more wishes");
    
    genie.Dump();
} 

The results will look like:

Image 2

Wham, bam, thank you ma'am. I'm done testing my class, have some visual output, and off I go into my next project.

Good Old Visual Studio Solution

The usual steps will look something along these lines:

  • Create a new Console solution
  • Paste the same code from the linqpad main method into your console main method
  • Realize you don't have .Dump()
  • Paste the Genie class into your solution
  • Override the ToString() so you can have some output
    C#
    public override string ToString()
    {
        return string.Format( "YearsInBottle:{1}{0}WishesToGrant{2}{0}Color:{3}{0}"
                            , Environment.NewLine
                            , YearsInTheBottle
                            , WishesToGrant
                            , Color                                               );
    }
    
  • Change all the genie.Dump() to Console.Out.WriteLine(genie);

Congratulations, you can now run your project and you'll get something like:

Image 3

The Empire Genie Strikes Back

A workaround would be to simply have an extension method which will print your Genie object. The problem with this approach is that it's not generic, and you'll have to do this to each class you want to debug.

You can try to write Dump() from scratch (or maybe ask the author to give you the code) but both options seemed a bit drastic to me.

You can also use refelction to get all the properties and their values from the object, deal with a few end cases and format it yourself. The downside is the usual "reflection is slow", and the code isn't the most pretty either (yes, Newtonsoft will probably be doing something like that, but it's already been written, tested, and tuned for performance).

Here's another solution:

1. Install NewtonSoft JSON

Head into your nuget package control, and install NewtonSoft JSON (You don't have to stick with version 6 obviously, I believe version 10 is out by now).

Image 4

2. Create a new file called ExtensionMethods.cs in your console project.

In this file, we'll have our extension method for the Dump(). If you're wondering if this is not too radical, the answer is no. It's modular, and I'm usually having other extensions methods that I need for a particular project, so they'll all live happily ever after in this file.

Your file should look like this:

C#
using System;
using Newtonsoft.Json;
 
namespace JSON_Dump
{
    public static class ExtensionMethods
    {
        /// <summary>
        /// This is a simple (and lazy, read: effective) solution. Simply send your
        /// object to Newtonsoft serialize method, with the indented formatting, and
        /// you have your own Dump() extension method.
        /// </summary>
        /// <typeparam name="T">The object Type</typeparam>
        /// <param name="anObject">The object to dump</param>
        /// <param name="aTitle">Optional, will print this before the dump.</param>
        /// <returns>The object as you passed it</returns>
        public static T Dump<T>(this T anObject, string aTitle = "")
        {
            var pretty_json = JsonConvert.SerializeObject( anObject, Formatting.Indented );
            if ( aTitle != "" )
                Console.Out.WriteLine( aTitle + ": " );
 
            Console.Out.WriteLine( pretty_json );
            return anObject;
        }     
    }
}

(I keep forgetting that pasting stuff from VS keeps the formatting ... awesome ... ).

All this does is take an object (we don't care about the type), gives it to Newtonsoft.Json, and asks: "Can you pretty please make this object pretty?". Newtonsoft Json will convert your object to JSON, and then indent it for you, free of cost. We'll output it to the screen, and return the object just as we got it, so you can still use it in an example like the above.

3. Keep using your Linqpad code (including the .Dump())

Now you can just paste your Linqpad code into your main, and it'll work. No changes necessary. :)

Here's how it looks after pasting it and running the code:

Image 5

As you can see, you get nice JSON object notation, it's indented, and if you have inner object, they'll be indented even further.

The Good News

This is easy, and doesn't involve lots of work on your part. You can simply paste the extensions file into your project, or if you want, you can have all your usual extensions method compiled into a DLL, and just include that (I have done this, since I have some other nifty extension methods I usually use).

The Bad News

If you haven't learned this yet, nothing is free in life. You get the benefit of not worrying about your ToStrings overrides, but you pay in disk space. Newtonsoft DLL (which will be copied into your output folder) clocks in at 433kb, and the XML that goes with it another 435kb. This is not serious, especially if you're using Newtonsoft Json for other things in your project, but if you're aiming to ship a small executable, you might want to stick with overriding the ToString(). :)

Wrap Up

Hopefully, you'll find this useful, and even if not, I hope it gave you some interesting things to think about.

If you would like to learn more about Newtonsoft Json, head to the project homepage.

If you would like to learn more about extensions methods, head to this MSDN page.

If you've found this article helpful, please vote for the article, leave a message, and feel free to post back to this article. Smile | :)

History

  • 24 June, 2014: Initial release
  • 29 June, 2014: Fixed the Anchor tag code and some formatting
  • 8 May, 2015: Another round of formatting and reading enhancements
  • 29 September 2017: Typo plus another option added.

License

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


Written By
Software Developer
Australia Australia
Coding since I Remember myself ... went through Basic on Commodore 64 to C# on a 24 cores AMD cpu... In between worked with c, c++, java, assembler, php, pascal, JavaScript, SQL based DB's and a bit of NoSQL as well.

Love software, and I'm usually fidgeting around with technology software and hardware on my free time.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1202102828-Sep-17 2:22
Member 1202102828-Sep-17 2:22 
GeneralRe: My vote of 5 Pin
_Noctis_28-Sep-17 23:27
professional_Noctis_28-Sep-17 23:27 
GeneralMy vote of 5 Pin
Volynsky Alex30-Jun-14 5:32
professionalVolynsky Alex30-Jun-14 5:32 
GeneralRe: My vote of 5 Pin
_Noctis_30-Jun-14 15:08
professional_Noctis_30-Jun-14 15:08 
GeneralRe: My vote of 5 Pin
Volynsky Alex30-Jun-14 21:27
professionalVolynsky Alex30-Jun-14 21:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.