left-icon

LINQPad Succinctly®
by José Roberto Olivas Mendoza

Previous
Chapter

of
A
A
A

CHAPTER 7

General Summary

General Summary


LINQPad is a software utility intended to be used as a part of .NET applications development. It is used for interactively querying SQL databases using LINQ, as well as interactively writing C# code without the need for an IDE, such as Visual Studio. LINQPad supports the VB.NET, SQL, and F# languages. LINQPad also supports Entity Framework, LINQ to Objects, LINQ to SQL, and LINQ to XML interoperability.

There are three versions of the product: LINQPad 2.x (for .NET Framework 3.5), LINQPad 4 (for .NET Framework 4.0/4.5), and LINQPad 5 (for .NET Framework 4.6). There are four editions for each of these versions: Free, Pro, Developer, and Premium.

LINQPad enables extra features for the paid license versions. Some of these features include full C#/F#/VB auto completion and tooltips, code outlining and formatting for C#/VB, built-in and custom code snippets, and full NuGet integration.

LINQPad is a client/server application that works as one client and many servers. In this case, the LINQPad user interface is the client, and for each query to be executed, LINQPad creates a separate server. For communications between client and servers, LINQPad uses a customized communications layer that runs using Windows shared memory.

LINQPad uses third-party libraries from several software publishers within the UI. These includes Actipro’s SyntaxEditor, the SharpDevelop project, Microsoft Roslyn, and NRefactory. The libraries from these publishers are included as embedded resources in LINQPad, so it ships as a single executable. All updates for the product are downloaded into the application’s data folder. Every time it’s started, LINQPad always checks to see if there’s a later signed version of itself in the application’s data folder.

LINQPad software can be downloaded from the following locations:

After you download LINQPad, double-click the LINQPad4Setup.exe (or LINQPad5Setup.exe) file to begin the installation.

The installation process is guided by a series of dialog boxes that allow you to select the program’s location folder, files associations, icons creation, and environment variables setting for command line support. When LINQPad installation is complete, a set of folders and files are created that include queries, plugins, code snippets, data context drivers, connections, default references, and user preferences.

LINQPad can also be deployed as a portable application. To accomplish this task, a special directory structure should be created in a USB memory stick, including the linqpad.exe file within it.

To start working with LINQPad, double-click on the icon placed on the Windows desktop. This will launch the LINQPad user interface, which is the client application that will do most of the work.

For the purposes of this book, we identified 14 graphic elements in the LINQPad user interface: the LINQPad main menu, Connection’s tree view area, Queries and Samples tree view area, Query code tab window area, Query code tab caption, Add New Tab button, Execute Query button, Stop Execution button, Results View Type button, Language Selector combo box, Connection Selector combo box, Close Tab button, Premium Features activation link, and Status Bar.

The editor window is one of the user interface elements that can be customized. To do so, select the Preferences command from the Edit menu, and the settings for the window can be changed in the Editor tab of the Preferences dialog box. For the purposes of this book, the Segoe UI font was used, and also the code line numbers were displayed, too. Font size customization is not available because LINQPad allows the user to increase or decrease the font size using Ctrl+Plus (increase) or Ctrl+Minus (decrease), or by using the mouse’s scroll wheel while pressing the Ctrl key.

When working with LINQPad, a basic concept is the term query. A query is a piece of code that can be executed immediately. All written queries are displayed beneath a tree view node called My Queries. Physically, LINQPad stores queries in a folder named LINQPad Queries, located into the user’s Documents folder. The location of this folder can be changed by clicking the Set Folder hyperlink in the user interface, and browsing for a new folder in the Preferences dialog box. All queries discussed in this book were saved in a folder named C:\LINQPad_Succinctly\Queries.

LINQPad allows you to search queries by using a term. This can be done by clicking on the Go to hyperlink located on the My Queries tree view node in the user interface. Then, you can type the term in the Navigate to Query dialog box, and LINQPad will show all queries that match that term.

LINQPad queries can be written in 10 different programming modes: C# Expression, C# Statements, C# Program, VB Expression, VB Statement, VB Statements, VB Program, SQL, ESQL, F# Expression, and F# Program. Each of these languages can be selected in the Language selector combo box, located at the top of the editor window.

All query filenames are associated with the .linq file extension. Internally, a header in XML format is added at the beginning of every query file. This header tells LINQPad which kind of programming language is used to compile the query, the connection used by the query to retrieve data, and which assemblies and namespaces are referenced in the query. This XML header is not displayed in the editor window.

You can manage connections to SQL servers in the Connection’s tree view area by clicking on the Add Connection hyperlink. Then, the Choose Data Context dialog displayed on the screen allows you to choose which LINQPad driver will be used. For purposes of this book, the LINQ to SQL driver (which is the default) option was chosen for all queries.

After the LINQPad driver is chosen, the LINQPad Connection dialog box is shown to set the connection’s parameters. A Microsoft SQL Server connection was created, assuming that a SQLEXPRESS instance configured with SQL Authentication security mode was installed in the local computer.

LINQPad names a connection using the server name followed by a period and the name of the authenticated user when SQL Authentication is chosen. If Windows Authentication is selected, the name for the connection contains the server name only. Also, a context menu is available for all connections displayed in LINQPad user interface. This menu appears when the user right-clicks on a connection’s name. The menu contains a set of commands for managing the connection, including connection properties or connection refreshing.

LINQPad can be used to create database queries employing the default LINQ-to-SQL driver. We used a database named uspostalcodes in the samples discussed in this book. The database was created using the Choose Data Context dialog box, along with the LINQ to SQL Connection dialog box. Because there’s no way to create database entities with LINQ-to-SQL, Microsoft SQL Server Management Studio was used to create the database tables and to populate them with data.

The LINQ-to-SQL methods discussed in this book include the Take method, which returns a row set with a specific rows number, and the OrderBy method, which sorts the rows returned by a method. Some of these methods require a lambda expression in order to work properly. A lambda expression is composed by a parameter and an expression, which takes the parameter and uses it to return a value. The term lambda expression comes from lambda calculus, which is a mathematical notation applied for defining functions. The methods discussed in this book that use a lambda expression are: OrderBy (to sort rows), Where (to filter rows based on a conditional expression), Sum (to summarize the value of a given column), Average (to get an average from a series of values of a given column), and GroupBy (to group rows based on the value of a given column).

LINQPad provides a special class named PredicateBuilder. This class is intended to simplify queries writing when a complex filter condition is needed. The class solves this issue by providing a set of special methods intended to act as logical comparison operators. Because PredicateBuilder is a generic class, it can receive any entity reference of the database used by a connection. The value proposition for the PredicateBuilder class resides in its ability to build any lambda expression dynamically when the instance of the class is passed to any LINQ-to-SQL method. The expression is created using the values passed to the class instance using the And or the Or methods. Both methods should be called repeatedly before using the instance. For using PredicateBuilder class in queries, you must add a reference to the class into any query which employs it. This can be done by pressing F4 and checking the Include PredicateBuilder check box that appears in the Query Properties dialog box.

One of the most valuable features of LINQPad is the ability to use it as a test and prototyping tool. LINQPad can run code in the C#, F#, and VB languages for that purpose. Each one of these languages can be executed as a single-line expression, as several sentences, and as a program. In all cases, the performance for a given code can be measured by reading the time consumed by the execution from the status bar. This feature allows developers to improve code before a product’s release. LINQPad also allows the use of custom assemblies in queries. These assemblies can be referenced by selecting the Query Properties command from the Query menu, or by pressing the F4 key. The user can pick up an assembly from those installed in the system, or browse for a custom assembly file into the local filesystem, or both. Users of the LINQPad Premium edition also can reference NuGet packages, while the users of the Free edition can reference only those packages used in LINQPad examples.

When custom or system assemblies are referenced in queries, the user should declare the classes or methods belonging to those assemblies by an explicitly qualified name. That is, to use the Button control located in the System.Windows.Forms namespace, the user should write System.Windows.Forms.Button in order to create an instance for the class. To simplify the way that the code is written, LINQPad allows you to import namespaces into queries using the Additional Namespace Imports tab located in the Query Properties dialog box.

LINQPad uses a method called Dump to display the output produced by a query. This method renders any kind of object, such as Windows Form control, an image from a web location, or the results of a LINQ-to-SQL query.

Another useful class shipped with LINQPad is the Util class. This class has many useful methods and properties that can be used to interact with queries in order to run system commands, obtaining all available queries in a list, highlighting the results for a query, exporting results to a CSV file, or displaying progress bars.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.