views:

48

answers:

2

Does anyone have a complete list of LINQPad extension methods and methods, such as

.Dump() SubmitChanges()

Much appreciated!

+3  A: 

LINQPad defines two extension methods (in LINQPad.Extensions), namely Dump() and Disassemble(). Dump() writes to the output window using LINQPad's output formatter and is overloaded to let you specify a heading:

typeof (int).Assembly.Dump ();
typeof (int).Assembly.Dump ("mscorlib");

You can also specify a maximum recursion depth to override the default of 5 levels:

typeof (int).Assembly.Dump (1);              // Dump just one level deep
typeof (int).Assembly.Dump (7);              // Dump 7 levels deep
typeof (int).Assembly.Dump ("mscorlib", 7);  // Dump 7 levels deep with heading

Disassemble() disassembles any method to IL, returning the output in a string:

typeof (Uri).GetMethod ("GetHashCode").Disassemble().Dump();

In addition to those two extension methods, there are some useful static methods in LINQPad.Util. These are documented in autocompletion, and include:

  • Cmd - executes a shell command or external program
  • CreateXhtmlWriter - creates a text writer that uses LINQPad's Dump() formatter
  • SqlOutputWriter - returns the text writer that writes to the SQL output window
  • GetMyQueries, GetSamples - returns a collection of objects representing your saved queries / samples (for an example, execute a search using Edit | Search All)
  • Highlight - wraps an object so that it will highlight in yellow when Dumped
  • HorizontalRun - lets you Dump a series of objects on the same line

LINQPad also provides the HyperLinq class. This has two purposes: the first is to display ordinary hyperlinks:

new Hyperlinq ("www.linqpad.net").Dump();
new Hyperlinq ("www.linqpad.net", "Web site").Dump();
new Hyperlinq ("mailto:[email protected]", "Email").Dump();

You can combine this with Util.HorizontalRun:

Util.HorizontalRun (true,
  "Check out",
   new Hyperlinq ("http://stackoverflow.com", "this site"),
  "for answers to programming questions.").Dump();

Result:

Check out this site for answers to programming questions.

The second purpose of HyperLinq is to dynamically build queries:

// Dynamically build simple expression:
new Hyperlinq (QueryLanguage.Expression, "123 * 234").Dump();

// Dynamically build query:
new Hyperlinq (QueryLanguage.Expression, @"from c in Customers
where c.Name.Length > 3
select c.Name", "Click to run!").Dump();

You can also set up your own extensions methods for use in LINQPad. The first step is to write and test your extension methods - you can do that in LINQPad by setting the query language to 'Program':

void Main()
{
  "hello".Pascal().Dump();  
}

public static class MyExtensions
{
  public static string Pascal (this string s)
  {
    return char.ToLower (s[0]) + s.Substring(1);
  }
}

The next step is to paste those extensions methods into a class library in Visual Studio. After compiling your library, go back to LINQPad and press F4 to add a reference to that library - and click Use as default for new queries.

Joe Albahari
Nothing better than an answer from the author himself!
John
Maybe I'll end up writing the manual via StackOverflow :)
Joe Albahari
Thanks, this was helpful!
Bent Rasmussen
Joe, I actually wanted to prototype some graphics work as well and then wanted to dump out a bitmap; it'd be great with a Show method for this kind of work where you want some visualization, working on Graphics, Image, etc. Possibly providing neat visualizations for some other types down the road.
Bent Rasmussen
...Actually, as long as you can send graphics to the output panel, we can build extensions for the rest ourselves.
Bent Rasmussen
The 4.26 beta lets you inject XHTML into the output stream, by calling Util.RawHtml. Go to www.linqpad.net/beta.aspx (or wait a few days for RTM).
Joe Albahari
A: 

Dump is a global extension method and SubmitChanges comes from the DataContext object which is a System.Data.Linq.DataContext object.

LP adds only Dump and Disassemble as far as I'm aware. Though I would highly recommend opening it in Reflector to see what else is there that can be used. One of the more interesting things is the LINQPad.Util namespace which has some goodies used by LINQPad internally.

John