Learn To Use LINQPad

Introduction

 
In today’s article we will look at an extremely useful tool that you can use to supplement your .NET development efforts. Every developer must develop some proof of concept at some stage of a development project. This could be some small code snippet etc. for which we always used simple console applications to understand and clean up some code logic. This exercise was not only limited to proof of concepts but also during actual development we all run into situations where we need to test some piece of code to improve it. Now, we have a tool called LINQPad that can make this process quite easy for us. We will look at this tool in this article.

Downloading LINQPad

 
The first step is to download this tool. You can download it from this location.
 
Here, you can choose the version based on your development needs.
 
Learn To Use LINQPad 
 
Once downloaded and installed you can run and see the main screen as below,
 
Learn To Use LINQPad 

Running various code snippets in LINQPad


To see the various items that we can run from LINQPad, let us use the samples that are included with the tool. We will walk through these samples to get an idea of what elements can be run and tested from the tool.
 
Let us start with the Hello LINQPad! Example. When we select and run this query, we get the below,
 
Learn To Use LINQPad
 
Learn To Use LINQPad 
 
This is a remarkably simple query. In the top we can see that the language is set to “C# Expression”. Any type of C# expression can be tested here. The next example is a C# expression again, but this is slightly complex as compared to the simple hello world example and lists words in order of their letter count as below,
 
Learn To Use LINQPad 
 
Let us try our own query and see the results as below, 
  1. System.Convert.ToInt32("500")  
Learn To Use LINQPad 
 
Next, we look at C# Statements with the “Multiple statements” as below,
 
Learn To Use LINQPad 
 
Here, we see that multiple statements were evaluated, and the results returned to us. This is an especially useful type of language provided by LINQPad as we can test multiple statements from our code as below:
  1. var x = 7;  
  2. varisTrue = ((x <10) &&( x>5)) || ((x >20) && (x <25));  
  3. isTrue.Dump();  
Learn To Use LINQPad 
 
Next, we look at running C# programs using the “Custom methods and types” as below,
 
Learn To Use LINQPad 
 
We now create our own C# program and run it as below, 
  1. void Main()  
  2. {  
  3.     var taxCalculator = newTaxCalculator(50000);  
  4.     var taxCalculated = taxCalculator.getTax().Dump();  
  5. }  
  6.   
  7. public class TaxCalculator  
  8. {  
  9.     int _salary;  
  10.     public TaxCalculator(int salary)  
  11.     {  
  12.         _salary = salary;  
  13.     }  
  14.   
  15.     public double getTax()  
  16.     {  
  17.         return _salary * 0.25;    
  18.     }  
  19. }  
Learn To Use LINQPad 
 
Next, we move onto one of my favorite features of LINQPad and that is to query a database. The default installation of LINQPad comes with a sample database called DemoDB and this is used in the sample “What about querying a database” as below,
 
Learn To Use LINQPad 
 
Here you see that the connection is populated with the demo database connection. We can add our own connection strings to point to our own databases and query these here. Also, by clicking on the SQL tab ahead of the results tab, we get the SQL query generated as below,
 
Learn To Use LINQPad 
 
Let us try to write a SQL query directly, 
  1. select * from Categories  
Learn To Use LINQPad 
 
Here we see that we can see the results of our SQL query.
 

Summary

 
In this article, we looked at some basic applications of the LINQPad tool. This is an extremely useful tool for the .NET and C# developer. There are many other features of this tool which I did not cover in this article. I would suggest you look through all the samples and help documentation to learn all other great features of this tool. Happy Coding!