Getting class files from LinqPad

There’s no end of tools that can create C# class’s from a database. But it’s never as easy or as flexible as it should be.

Two tools that I trust are LinqPad and dotPeek. I use LinqPad to generate the dll that contains the required classes and dotPeek to extract the class files from the dll.

Going all the way to dotPeek is really only viable if there’s a large number of class files you want. If there’s just one or two, IPSpy is all you need.

Here’s how to do it.

  • Establish a database connection as normal in LinqPad

  • Then “Reflect Query in ILSpy”, at this stage, no query needs to be created, just the db connection, (or press Alt+Shift+R)

ReflectQuery.png
  • In ILSpy, navigate to “TypedDataContext_xxxxx

DataContext.png
  • Unfortunately, ILSpy doesn’t have a feature to export the class’s to cs files. However, you can use the free JetBrains dotPeek to decompile the dll created by Linqpad and create yourself a C# project,

    • From ILSpy, right click the required namespace and select open containing folder, this will show you where the dll is.

    • Using dotPeek, import the dll, right click the dll, and select “Create new Project”