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.
Once downloaded and installed you can run and see the main screen as below,
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,
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,
Let us try our own query and see the results as below,
- System.Convert.ToInt32("500")
Next, we look at C# Statements with the “Multiple statements” as below,
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:
- var x = 7;
- varisTrue = ((x <10) &&( x>5)) || ((x >20) && (x <25));
- isTrue.Dump();
Next, we look at running C# programs using the “Custom methods and types” as below,
We now create our own C# program and run it as below,
- void Main()
- {
- var taxCalculator = newTaxCalculator(50000);
- var taxCalculated = taxCalculator.getTax().Dump();
- }
-
- public class TaxCalculator
- {
- int _salary;
- public TaxCalculator(int salary)
- {
- _salary = salary;
- }
-
- public double getTax()
- {
- return _salary * 0.25;
- }
- }
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,
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,
Let us try to write a SQL query directly,
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!