15

I work with a lot of Linq queries in my code, and I'm looking for a library that provides a formatted Dump() function similar to what LinqPad offers. LinqPad's Dump() extension method is really quite nice, because it handles nested collections very well.

Ideally, it would print out pretty tables in plain text, but I'd be ok with spitting out HTML or other nicely formatted data.

The ObjectDumper sample from VS does not cut it at all.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Garrett Serack
  • 943
  • 7
  • 17

2 Answers2

21

This is what I've been using:

Special thanks to this thread (especially Pat Kujawa's & anunay's comments)

C# (Straight from Pat Kujawa's comment (though I made it return itself so that it chains like linqpad's version does)):

public static T Dump<T>(this T o) {
    var localUrl = Path.GetTempFileName() + ".html";
    using (var writer = LINQPad.Util.CreateXhtmlWriter(true))
    {
        writer.Write(o);
        File.WriteAllText(localUrl, writer.ToString());
    }
    Process.Start(localUrl);
    return o;
}

VB (my conversion since I needed it in a VB app):

Public Module LinqDebugging
    <System.Runtime.CompilerServices.Extension()>
    Public Function Dump(Of T)(ByVal o As T) As T
        Dim localUrl = Path.GetTempFileName() + ".html"
        Using writer = LINQPad.Util.CreateXhtmlWriter(True)
            writer.Write(o)
            File.WriteAllText(localUrl, writer.ToString())
        End Using
        Process.Start(localUrl)
        Return o
    End Function
End Module

You will need to add the linqpad executable as a reference in your project as well as System.IO and System.Diagnostics

This launches your default web browser showing the exact output that linqpad would generate.

diceguyd30
  • 2,742
  • 20
  • 18
  • 7
    To make referencing LINQPad.exe easier, there's now any AnyCPU build for both the Framework 3.5 and 4.0 builds: www.linqpad.net/beta.aspx – Joe Albahari May 18 '11 at 01:27
  • I wonder if the usage of this dump-method inside your own code violates til eula? From [Linqpad eula](http://www.linqpad.net/eula.txt): Usage You may redistribute an unmodified version of the LINQPad executable with a commercial or non-commercial application or library for the sole purpose of allowing end-users to run LINQPad. Redistribution for other purposes requires written permission from the licensor. – Nikolaj Mar 09 '16 at 10:34
  • 1
    I would recommend only using this function for debugging issues. Whenever we built the final product LINQPad was listed as "do not include" in the project references. – diceguyd30 Mar 18 '16 at 22:13
4

As diceguyd30 points out, you can actually access the LINQPad executable directly in your code and have it produce the HTML itself. This would work best if you're trying to output the HTML to the interface as part of normal execution of your program.

If your purpose is to produce debug data that you can monitor while your program runs, another option is to use the Console.Write(object) method, and then set your Console.Out to something that can format objects intelligently. For example, you can reference your executable from LINQPad, and use it to execute a method that you're debugging, and LINQPad will treat any Console.WriteLine(object) calls the same as it would a call to object.Dump().

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315