Developer Tools - LINQPad

LINQPad is a powerful development tool that I as a developer cannot do without. The application has replaced the console when writing and trying out new code. Some of the things you can do with LINQPad are:

  • Query databases using SQL, LINQ and Entity Framework
  • Reference .Net Dlls and Nuget Packages
  • Add Database Connections and directly query / update the database
  • View the structure of tables and access the schema
  • Write classes or functions to test results
  • Write using C#, VB.Net or Sql
  • Test theories by writing a quick function instead or running a whole application
  • Using the Dump() function to output results visually
  • Using the Sql tab to see the generated SQL by LINQ

Where to Get LINQPad

LINQPad can be downloaded from linqpad.net. There is a free version and a paid version. With the paid version you get Nuget Packages and autocomplete amongst other features.

Setting up LINQPad

In the code articles unless otherwise stated the LINQPad query language is set to C# Program. This allows the query to run from Main() and then classes and other libraries can be called as if you were running a console application.

Linqpad scripts are called queries and can be saved and reused.

Adding a Database Connection

One of the first things to do is to reference the database connection that you want to query against. In LINQPad it will create a LINQ to SQL connection that allows you to query the database from inside LINQPad.

  • In the right-hand pane click on Add Connection
  • Choose Linq-To-SQL driver from the dialog box
  • Click Next
  • You will then see a database dialog where you can add the server \ connection name and credentials. Add these in and click Test Connection
  • Click Ok
  • You will now see you have the database connection listed.

Clicking on the connection you can now see the tables, views and stored procedures. If you have different schemas then LINQPad will show the items under each schema.

Adding Nuget Packages to Your Query

When you have a query open with LINQPad you may want to add a nuget packageAn example would be adding Newtonsoft.Json.

  • Press F4 whilst in a LINQPad query
  • Click on the Add Nuget Package Button
  • In the Dialog Box that pops up enter the package name to search for (if not installed).
  • Once the package is downloaded you can click on the package and it will be added to your query.
  • A further step then is to add the namespaces. You can do this from the dialog by clicking on add namespaces or manually as a step later on.

Adding Existing Dll's to Your Query

To add a library that you have written or to reference another follow these steps:

  • Press F4 whilst in a LINQPad query
  • Click on Add\Browse
  • From the Open File Dialog select the DLL that you wish to reference
  • Click Ok and then add the using statements (see below)

Adding Using Statements to a Query

To add \ remove using statements in a LINQPad query you need to use a dialog.

  • Press F4 whilst in a LINQPad query
  • In the dialog that pops up select the tab Namespace Imports
  • You can either type the namespace manually
  • The Syntax is the namespace name without the word using or the semicolon on the end
  • Or you can click on Pick From Assemblies, Select the Assembly then double click on the available namespaces

Setting Up LINQPad to run against the Database

When you create a new query in LINQPad there are two dropdowns that are important

  1. Language - This gives you the option to test a single line, script, or program in VB, C#,SQL etc
  2. Connection - If left blank then no database connection is added. Otherwise the database connected to

To use the database of Entity Framework connection select it from the Connection dropdown. You can then use the table names directly (pluralised by LINQPad) or use the this object in the query to access the connection object.