LINQPad is a software utility targeted at .NET Framework development. It is used to interactively query SQL databases using LINQ, as well as interactively writing C# code without the need for an IDE. This expands its use to a general “test workbench” where C# code can be quickly prototyped outside of Visual Studio.
The user interface is the client, as you might expect. For each query, LINQPad creates a separate server, which a class that runs in its own process and executes the query in isolation. This isolation prevents queries from interfering with each other (or the UI) and allows LINQPad to safely cancel a query without polluting other application domains.
In the past, LINQPad used in-memory Remoting channels to communicate with the server processes. Now it uses a custom-written communications layer that runs atop Windows shared memory (yes, there are plenty of pointers!) This provides a faster, more reliable, and fully asynchronous communications layer.
LINQPad 5+ compiles your queries using the Microsoft Roslyn libraries (in the past it used .NET’s CSharpCodeProvider and VBCodeProvider). Because C# and VB are statically typed, any database objects that you reference need the backing of a typed DataContext. For performance, LINQPad builds typed DataContexts on the fly – either using Reflection.Emit – or by generating and compiling source code.
Because so much work goes on the background (querying database schemas, emitting typed DataContexts, compiling and then executing queries), every potentially time-intensive feature of LINQPad must operate asynchronously to maintain a responsive user interface.
LINQPad’s query editor uses Actipro’s SyntaxEditor control (a very polished product). Features such as syntax highlighting, red squiggly underlines and autocompletion require that you lexically and semantically parse the source code. Lexical parsing means reading the raw text stream and breaking it up into a stream of tokens; a semantic parser then reads those tokens and figures out what they mean in context, emitting a code DOM (called an Abstract Syntax Tree). The final step is to resolve the nodes in the AST into .NET types and members (binding).
LINQPad 5+ uses the Microsoft ‘Roslyn’ libraries for parsing, binding, and compiling. (Prior to this, the heavy lifting was done by NRefactory and SharpDevelop.Dom, written by Daniel Grunwald, Andrea Paatz, and Mike Krüger, as part of the SharpDevelop project).
The debugger uses an adapted version of Microsoft’s Managed Debugger sample. Two additional layers sit on top of this, which are part of LINQPad. The first provides a lazy, asynchronous, cancellable, mostly immutable layer which can self-heal references that have been moved by the GC. The second is a WPF View-Model, which synchronizes with the layer below to provide bindable objects to display in the list views. The watch window expression evaluator uses the NRefactory library.
Because LINQPad ships as a single click-and-run executable, all the libraries are included as embedded resources.
A bonus of this approach is that you can compress the libraries at the same time, reducing the executable size.
LINQPad automatically patches itself by downloading updates into its Application Data folder. It then checks that the new assembly has a valid signature, and if so, forwards to that executable, which then writes itself back to the original file. This may fail (depending on the permissions of the folder to which LINQPad was downloaded) so LINQPad always checks whether there’s a later signed version of itself in its Application Data folder when started.
LINQPad offers a series of benefits to the user, such as:
- Full support for C# 6 and later, and VB 14 in LINQPad 5.
- Reporting of execution time in its status bar, allowing performance testing.
- Cloning of code snippets with a keyboard shortcut (Ctrl+Shift+C), allowing their execution side by side.
- Custom extension writing, which allows users to code methods and classes to make them available for all LINQPad queries.
- Custom assembly referencing.
- Building typed data contexts on the fly, including drivers and formatters for SQL Server, SQL Azure, SQLCE, Oracle, SQLite, and MySQL.
- Object rendering with the embedded Dump method.
- Access control for the Dump method in users’ custom types.
- Full command-line experience with the lprun.exe utility.
- Support for writing custom visualizers.
- Support for presentation mode, allowing use of cordless presenter devices.
LINQPad’s autocompletion includes everything that you depend on:
- Member lists & completion, with list filtering and “camel humps”
- Parameter listing
- Quick info
- Code outlining
- Smart tags for importing additional namespaces and references (C#/VB)
- Automatic code formatting (C#/VB)
Autocompletion works not only with LINQ queries, but with the entire C#/F#/VB language.
LINQPad’s unique Power-Tick lets you select multiple properties, when creating anonymous types in C# or VB, without closing the popup. This is a massive time-saver.
When you enter an unresolved type, LINQPad instantly scans all .NET assemblies and offers a smart tag to add the namespace and assembly reference. And you don’t even have to get the casing correct! So, if you type protecteddata, LINQPad’s smart tag will import the System.Security.Crytopgraphy namespace, add a reference to System.Security.dll if required, and correct the case.
The Developer and Premium editions also enable your favorite Visual Studio C#/VB code snippets, plus a dozen unique high-productivity LINQ snippets – as well as any custom snippets that you define.
The Developer/Premium editions also unlock NuGet integration, so you can effortlessly reference anything in the entire NuGet library, and the ability to write cross-database queries - just control+drag additional databases from the Schema Explorer onto your query!
And these editions also let you open database tables for editing, saving changes back to the database.
And with a premium license, unlock an integrated lightweight debugger. Single-step through your scripts, set breakpoints, and examine local variables! The debugger works with all CLR languages: C#, VB and F#.
The premium license also enables advanced code fixes, including override method and implement interface/abstract class.
Entity Framework (EF) and LINQ are powerful tools that make the job of developing software applications much easier. EF provides an abstraction layer to the underlying data source, while LINQ to SQL allows you to easily query it. The alternatives to LINQ are SQL queries or stored procedures, which can be tedious to maintain and can add significant overhead to the software development life cycle. For example, unless you use EF Migrations or something of the like, you might have to use a separate source control mechanism to manage versions of your SQL scripts. In fairness, SQL generally has faster execution times than LINQ.
With LINQ, we can write complex queries using tools and languages (C#, Visual Studio, ReSharper, etc.) most developers are familiar with. Yet, sometimes there are situations where we don’t quite understand an expression, or we need to isolate some part of a query to debug it. In those instances, LINQPad can be very handy. A small utility, available both in free and different premium editions, LINQPad allows us to write test queries against EF or a variety of other sources, such as OData or WCF services.
Next will show how to quickly set up a connection in LINQPad so we can write queries using EF and LINQ.
First, we need to setup a connection. Click on Add Connection.
In the Choose Data Context window, select EntityFramework (DbContent V4/V5/V6) and click next.
In the Connection window, specify the parameters of the connection:
Path to Custom Assembly: The path to the dynamic link library (DLL) that contains the public class inheriting DbContext(). Use the Browse link to locate it in the file system. Hint: In Visual Studio, right click on the project and select “Open folder in File Explorer.” Most likely, the DLL is located in the bin folder of the project.
Full Type Name of Typed DbContent: Using the choose link, select the class with the context that contains the DbSet we want to query.
How Should LINQPad Instantiate Your DbContext: This setting depends on how your project is set up. In this example, I am using a connection string in the web.config file.
Path to the Application Config File: This is the path of the config file that contains the connection string.
Production Database: If this option is selected, when you run a potentially dangerous query, LINQPad will warn you that you are working on a production database.
Remember this Connection: A self-explanatory setting—I suggest leaving it checked, unless there are security concerns.
Once the connection has been created, select it in the query tab. You can then write a simple C# statement with a query. In this example, we want to select the first 10 orders with related details. Click on the Execute button (or hit F5 on your keyboard) to run the code.
In the results pane, we see a graphical representation of our orderDetails object. That’s the result of the Dump() method of the ordersDetails object. Dump is an extension method provided by LINQPad. It prints the query result in the nice formatting shown above.
Another convenient feature of LINQPad is saving code snippets. If you write a query (or any other code) that is not so straightforward, you save it with a meaningful name in LINQPad for future reference. If, weeks later, you have to write something similar and you need to refresh my memory, you can quickly recall it without having to dig through several repositories and thousands of lines of code.
Sometimes simple things can make life easier. LINQPad is a simple application, yet very powerful, and it makes job much simple.
LINQPad 5 installation :
Download LINQPad 5, double-click the setup program file LINQPad5Setup.exe to open it. For most users, this setup program can be found in the Downloads folder.
How LINQPad is deployed in user’s computer
After LINQPad is installed, a set of folders and files is created. These folders and files hold all the necessary elements that make LINQPad work properly, and are displayed in the following table.
LINQPad user interface
The LINQPad user interface is where you will do most of your work. For the purposes of this book, we’ll identify 14 graphic elements of this interface. These elements are:
LINQPad File menu: Holds all available LINQPad commands for working with the program.
Connection’s tree view area: Shows all connections created by the user with the connection manager.
Queries and Samples tree view area: Shows all queries and extensions created by the user. It also shows all available samples within LINQPad, which are displayed in a separate tab.
Query code tab window area: Where the user writes query code.
Query code tab caption: Shows the name of the query being edited.
Add New Tab button: Adds a new tab for query editing.
Execute Query button: Executes the query that is currently selected.
Stop Execution button: Stops query execution.
Results View Type button: Allows the user to switch between Rich Text and Data Grid for viewing query results.
Language Selector combo box: Allows the user to select the programming language in which queries will be written.
Connection Selector combo box: Allows the user to select a previously created custom connection.
Close Tab button: Closes the tab currently selected.
Premium features activation link: In the free version of LINQPad, this link allows the user to open the product’s webpage to acquire a license for activating the paid license’s features.
Status Bar: Shows several messages sent by the program, including query’s time performance.
LINQPad basics: Queries
LINQPad uses the term query for every piece of code that is written in the editor. So from now on, this word will be used within this book for pointing to every code sample displayed in it.
We can save every one of these pieces of code for future use. By default, queries are saved in the LINQPad Queries folder located in the current user’s Documents folder. Also, after a query is saved, its name appears in the Queries and Samples tree view area of the user interface.
LINQPad is a free software utility developed by Joseph Albahari to be used as part of .NET application development. It’s used to interactively query SQL databases using LINQ, as well as to interactively write C# code without needing an IDE such as Visual Studio. This feature expands its use as a “test tool” where C# code can be quickly prototyped outside the IDE. It also supports the VB.NET, SQL, and F# languages. LINQPad supports the following dialects:
- Entity Framework
- LINQ to Objects
- LINQ to SQL
- LINQ to XML
LINQPad is a great tool which you can write and execute LINQ query against many data sources, like SQL Server, Oracle or Web Services. In addition to LINQ query support, the tool also supports C#/F#/VB expression, statement block or program, to write and execute the code and code snippet.
LINQPad is a fantastic tool that allows developers to quickly and easily execute LINQ queries against their database. Now with LINQPad 2.0+, LINQPad supports a plug-in model which means developers can couple the convenience of LINQPad with the power of LightSpeed!
Because the driver uses LightSpeed, you’ll be able to use all the facilities of LightSpeed — for example, to perform Lucene-based full-text search from within LINQPad, or to include or exclude soft-deleted entities from the query.
LINQPad supports everything in C# 3.0 and .NET Framework 3.5:
- LINQ to SQL
- LINQ to Objects
- LINQ to XML
LINQPad is also a great way to learn LINQ: it comes preloaded with 200 examples from the recently released C# 3.0 in a Nutshell. There’s no better way to experience the coolness of LINQ and functional programming.
And LINQPad is more than just a querying tool: it’s a code snippet IDE. Instantly execute any C# 3.0 or VB 9 expression or statement block!
Features in LINQPad :
- You can now include namespace declarations in queries of type ‘C# Program’, so you can paste entire .cs files from Visual Studio into LINQPad.
- LINQPad’s editor now supports bookmarks, to help with navigating around large documents. The shortcut keys are the same as the defaults in Visual Studio, and ReSharper’s ergonomic shortcut keys are also supported (Shift+Ctrl+1 through Shift+Ctrl+9 to set a numbered bookmark, and Ctrl+1 through Ctrl+9 to jump to a numbered bookmark).
- You can now add references to other .linq files, via the new #load directive.
- You can now connect to Oracle, MySQL, SQLite and PostgreSQL databases via a new built-in driver that leverages Entity Framework Core. The new driver generates data contexts with table and property names similar to the built-in LINQ-to-SQL driver (as well as LINQPad 5’s IQ driver), so that queries can be easily ported. It also supports SQL Server, so for SQL Server you can now choose between LINQ-to-SQL and Entity Framework Core.
- The default namespace imports for each query can now be viewed and edited. This can be useful in avoiding conflicts with other APIs.
- The extensibility model for writing custom data context drivers has been updated for LINQPad 6. Creating a project is now as simple as running a LINQPad script, and publishing it is as simple as publishing a NuGet package.
- When executing a selection in ‘C# Program’ mode, the selected text can now call other methods in the query.
- The ‘Go to definition’ shortcut (F12) now works for symbols defined in My Extensions (as well as #load-ed queries).
- The command-line runner and Util.Run are now available in LINQPad 6. The command-line runners are called LPRun6 (64-bit) and LPRun6-x86 (32-bit).
- Charting is back, as is the Roslyn syntax tree visualizer. And Roslyn Quoter is now integrated! To activate, either right-click from the editor, or click the new button on the syntax tree visualizer.
- Outlining now works in statements mode, too, if you have #regions or methods defined.
- If you hold down the control key anywhere in the editor, it will display the quick-info tooltip, as well as any errors or warnings on that line.
- If you type /// before a member, LINQPad will now expand it into a simple (one-line) XML summary. These summaries are picked up from #load-ed queries, as well as My Extensions.
- The back-end for the NuGet Package Manager has been re-written to be faster and more reliable. It now works directly from the local user cache, and recognizes reference assemblies and native dependencies in line with .NET Core protocols. When restoring packages, LINQPad 6 searches all enabled package sources.
- Queries now automatically re-load when the file is changed externally.
- When calling file-based methods such as File.Open, the editor now offers autocompletion on the file path.
- There are new options available when calling Dump – to suppress column totals, control repeating headers, and specify a depth at which to initially collapse the results.
- When compiling your queries, LINQPad 6 preferences reference assemblies over runtime assemblies. If the reference assemblies for the current .NET Core runtime version have not been installed, LINQPad will offer to download the appropriate NuGet package.
- LINQPad 6 supports soft cancellation. By monitoring this.QueryCancelToken, your query can respond to the Cancel button, and elect to end early without the underlying process being killed.
- You can now connect to SQL CE databases without installing SQL CE. LINQPad will automatically download the NuGet package as required.
- There’s now a checkbox in the Query Properties dialog to include references to ASP.NET Core assemblies.
- You can now rename a query tab (without saving it) by pressing Shift+F2, or via the option on the query tab’s context menu.
Major Features of LINQPad:
- There’s now a LINQPad driver for Entity Framework 7. Click Add connection and then View more drivers.
- The NuGet local package folder can now be overridden in LINQPad Nuget Package Manager | Settings. This can be useful in avoiding a PathTooLongException. LINQPad also now shortens NuGet folders with names longer than 40 characters, to lessen the chance of this exception.
- The DumpSyntaxTree and DumpSyntaxNode extension methods for Roslyn syntax trees now work with all versions of Microsoft.CodeAnalysis.
- LINQPad now supports Active Directory authentication for SQL Azure connections.
- The DataGrid results view has been improved in how it displays hyperlinks with collections and null values.
- Ctrl+W now closes the current query tab.
- When dumping sequences that throw exceptions, the partially enumerated sequence is now dumped.
- The ‘exclude’ option when calling Dump has been enhanced to let you to exclude all members except for ones that you specify. To use this mode, prefix the list with a plus sign.
What is LINQ