Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

LINQPad as a Code Snippet Execution Engine


:P
On this page:

If you haven't checked out LINQPad before it's probably high time. LINQPad is a cool little utility that was mainly created to allow you to test LINQ expressions and see them produce a result and output the results in a nice easy to visualize format. It's great for running LINQ Queries without having to fire up Visual Studio.

LINQPad does a great job at LINQ execution and visualization, but it has a lot more uses than just a LINQ query tool. It's basically a snippet editor that allows you to save snippets and more importantly, execute just about any code that you can write as an expression or single function block . You can execute both Expressions which is a single command that returns a result or a single block of commands or a Statement which is a block of commands in a single execution block (essentially what would amount to a method body).

It's awesome if you just want to very quickly verify how some .NET function or expression  works without having to write a test, console output routine or change your code and run it with the debugger.

For example, just a minute ago I was having some issues with a Path.Combine() in an application where the two path components passed in would be a path and filename, except that the filename could also have a leading slash and it was returning what I thought are unexpected results. Here's what this looks like in LINQ Pad after I needed to do some quick experiments with various different combinations of files and paths:

 LinqPadExecute

You can see the unfortunate result in the first result line in the Results panel which produces \test.tmp instead of the expected combined path. Actually the behavior is correct - the combination of the two paths sees no common path to combine so it just uses the latter part. Still I expected the function to assume I wanted to combine two paths regardless of the leading slash. <shrug>

Anyway, using LINQPad it was a piece of cake to paste the line and execute it without even touching the code I was working on in Visual Studio. LINQPad uses a .Dump() extension method on object to display the content of just about any structure to text output. It'll dump simple values with .ToString() and complex structures in an easy to visual tabular display. For example, above I added an anonymous type to the result which is shown in the table in the results pane above.

The cool thing is that it works with any code. There's even an Advanced option on the Query menu that allows you to import assembly references and namespaces so you can test your own assemblies.

Of course LINQPad - as the name implies - also works with LINQ queries and does a wonderful job of displaying LINQ and LINQ to SQL results:

LinqPadSqlResult

Output can be displayed both as a result table view, the raw SQL (from .ToString()) and a useful code conversion from the LINQ Expression syntax to LINQ command syntax using command chaining of the various LINQ extensions methods.

There's no installation - it's a single self contained EXE you can copy anywhere assuming you have .NET 3.5 installed.

If you haven't looked at LINQPad before - even if you're not using LINQ yet, be sure to check it out...This is one of those useful tools you just should always have around and "linqed" to a hotkey, in VS or as I do with SlickRun. It's a great way to quickly check out many things .NET.

Posted in CSharp  LINQ  

The Voices of Reason


 

Darrell
April 17, 2008

# re: LINQPad as a Code Snippet Execution Engine

I've just started toying around with Linq and have found LinqPad extremely useful. It's a great tool for learning Linq

Tyrone
April 17, 2008

# re: LINQPad as a Code Snippet Execution Engine

Using it...and Lovin it!! Thanks

Mike Gale
April 17, 2008

# re: LINQPad as a Code Snippet Execution Engine

Thanks for this.

This is one of those things that brings joy to programming.

Terry Aney
April 18, 2008

# re: LINQPad as a Code Snippet Execution Engine

I totally agree, LINQPad is mind blowing in terms of how easy it is to prototype code and make little utility scripts (and even LINQ to SQL query generation always brings a smile to my face when they 'nail' a complex query as good or better than I probably could have written it). I've added a few helpful extension methods to allow for batch deleting/updating with only one SQL statement, along with previewing the SQL for these commands before they execute. Check out: http://www.aneyfamily.com/terryandann/post/2008/04/LINQPad-Preview-DELETE-and-UPDATE-SQL-before-it-executes.aspx to see if they are helpful at all.

tom
April 20, 2008

# re: LINQPad as a Code Snippet Execution Engine

Cool, looks like VFP! :P

What About Thad?
April 23, 2008

# re: LINQPad as a Code Snippet Execution Engine

I *love* LINQPad and I've been wanting to spread the word about using it as a snippet executor, particularly vis a vis adding references to ones own code and ad-hoc testing. LINQPad also has a bonus under the samples that I just discovered yesterday: samples a small cookbook for RegEx... nice added value!

Lucas
April 23, 2008

# re: LINQPad as a Code Snippet Execution Engine

Yes! Finally one of the more popular bloggers writes about LinqPad! :P I've been using it since 3.5 was beta and it has saved me countless hours of creating a project/solution just to compile a short single-use program. I find it *very* useful...

- as a general "scripting" engine. i use it a *lot* at home to work with files (batch renames, organizing files into folders by extension, etc). for example, using an EXIF library, i can rename hundreds of pictures, add the date taken to the filename, and organize them into folders by day or month. i know there is software that does this, but it's only a few lines in LinqPad, and i like to do it myself :P

when debugging/troubleshooting an app i work on that relies heavily on photos stored in SQL Server:
- quick extension methods so i can grab a byte[] or System.Data.Linq.Binary from SQL and do this: bytes.ToImage()
- easily select/filter/sort by image details such as size in pixels or EXIF tags and get the results in a nice grid. try doing that with query analyzer!
- extension methods to do bytes.ShowAsImage() and display the image in a form, with "Save as..." button included.
- extracting binary data from SQL with a foreach loop and File.WriteAllBytes(bytes, path), or better yet, the extension bytes.WriteToFile(path), i can extract hundreds of photos stored in that DB

it got syntax highlighting (including errors) a few weeks ago, so now all i wish for is intellisense (coming soon).

Peter Bromberg
April 24, 2008

# re: LINQPad as a Code Snippet Execution Engine

I've been using LinqPad since I first got a hold of the Albaharis' C# 3.0 book, and I even turned on Jon Skeet to it! He was working on a LINQ visualizer at the time (impressive idea) using WPF.

Hot Topics
April 26, 2008

# Rick Strahl uses LINQPad for a lot more than just LINQ Queries

In his post LINQPad as a Code Snippet Execution Engine , Rick Strahl highlights the fact that LINQPad

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024