An Introduction To Expressions And The Tree View In LINQPad7

The purpose of this article is to introduce you to expressions and the Tree view in LINQPad7.

In this Article we will be focusing on the Tree view

The Following steps will be involved

  • When to use Expressions
  • What are Expressions?
  • Implementing the Tree View
  • Wrapup
  • Installation

    IMPORTANT: Before beginning this tutorial, be sure to download LINQPad to your computer.

    Download LINQPad

    LinqPad is a free tool with multiple priced tiers.

    I have found this tool to be very helpful for learning about backend operations from simple queries that you could use for your API calls, to writing triggers, unit tests, and more difficult backend concepts.

    For this example I will be using the version 7 with support for .NET 6. You may be prompted to install .NET 6, which is necessary for the program to run if you do not have it already installed.

    Please view my previous blog post – Setting Up LINQPad Toolset To Assist In Visualization And Manipulation of SQL Queries And Backend Data – to go over setup and using the Demo database.

    When to use Expressions

    When writing queries to make changes to our database, the language we use can become very difficult to read and write. In a professional setting the queries could become almost impossible to write by hand! To make this easier on ourselves, we can write an expression that will translate our query into SQL. I will be using the C# Language. When writing expressions that take the place of SQL queries, we call them query expressions. The query expressions follow a similar syntax to SQL.

    To do this we would need to create a LINQ provider (LINQ to SQL) to interpret the expression and convert it into SQL, but we can use LINQPad to simulate this and access the Tree view/SQL view to see our work clearly.

    Looking at LINQPad you can see that when we run Albums.Take(100), there is a language dropdown where we choose the language we work in. when using C# It also says “Expression”. An expression is our C# code that can be translated into SQL queries for our backend. Albums.Take(100) is a C# expression.

    What are Expressions?

    An expression is a combination of operands (a, c, d), operators (+, -, / ,* ), constants, and variables that are used together to get a value. A simple expression looks something like: a + b = result.

    There are 7 types of expressions we can use:

  • Constant Expressions: Consists of only constant, unchanging values (1+2)
  • Integral Expressions: Produces integer results (x + y = 13 where x and y are int var)
  • Floating Expressions: which produce floating point variables (x + y = 8.25 where x and y are floating point variables)
  • Relational Expressions: produce a bool variable, also called bool expressions (x <= y)
  • Logical Expressions: combine 2 or more relational expressions and produce a bool result (x <= y || x = 3)
  • Pointer Expressions: produces an address value (pnt++, pnt, &x where x is a variable and pnt is a pointer)
  • Bitwise Expressions: Is used when changing data at the bit level.
  • As you can see, we were able to accomplish our goal with less code as well as being more legible. As database tasks and their expressions gain complexity, the SQL does so exponentially.

    Tree View

    Expressions can be complicated in their own right. You may come across scenarios where you need to understand the difference between two different ways to write an expression that may seem to do the exact same thing. The Tree view can give a very helpful visual dissection of the actions that take place within our queries.

    To reach the Tree view, you need to run a query within the demo database. I will be running the Albums.Take(100) query to begin.

    We have 3 views within our Tree view. We have a folder layout view, the Tree view, and the Attribute Lists view.

    The Folder Layout: displays the query elements in expandable folder orientation. You can select a folder or item within at any level, and the other internal views will change their data and scope to match.

    The Tree Layout: Displays the query elements in a tree orientation and shows the order of the executed tasks as well as the results.

    The Table Layout: displays the data available at the selected portion of the query.

    At the top there are 3 colored words with 2 checkboxes.
    Syntax Node: This represents some kind of a class or object that can hold some kind of a value or type. These are immutable and cannot be removed from the view.

    Syntax Token: represents the “terminals of the language grammar”. They represent the smallest syntactic fragment of the code, and they can never be parents. We see above that each Syntax Token represents the syntax we used when creating the query expression.

    Syntax Trivia: stands for the data that is insignificant, like spaces or code comments.

    For our purposes I will be un-checking the Syntax Trivia box as it adds unnecessary bloat to the view.

    Begin by switching your language to C# Program and run the function below:

    Your Tree view should look overcomplicated like this:
    We want to view the expression tree for only var a but this view currently displays the entire code line including void Main(). Pieces of the function can be seen through the tree, but this can be tough to read. You can identify the equation by following the tree of nodes(blue) to the syntax(green) they match in our function.

    Use either the folder view to select the Add Expression Node, or just highlight only the Constant Expression(1 + 2) portion of the function. You should have something simple like this.

    When reading the Tree view, we always read from top to bottom for each vertical tree level, and left to right through the contents within the level. We start at the AddExpression Node which contains the Add Expression and its syntax we want to use (+). To get the numbers we want to add together we can go deeper into the AddExpression Node to get to the NumericalExpression Node and use the syntax for the number one (1) and two (2).

    If you switch input languages to something like C# Expression, the tree will change and look different, showing that the same code in different places is built differently, even if the overall syntax remains the same. This can be very useful when trying to be precise in the actions you take when doing something like calling a complex database for data. Some actions you can take can be almost identical, but have small niche cases that might matter a lot down the road.

    When revisiting the entire function, you can try working through the tree to identify the flow of all parts of the function!

    We can also checkout the C# Expression: Albums.Take(100)

    Wrap-up

    Expressions and their trees can seem daunting at first, but because of the Tree view, each piece of the expression’s inner workings and classes are identified by name and can be researched online for all its meanings and uses.

    Generally, the most popular use of expressions is to translate your backend language into SQL to make calls to a database. Expression trees are very helpful in understanding the makeup of the expressions you send or the translated queries you receive!

    About Intertech

    Intertech is a Software Development Consulting Firm that provides single and multiple turnkey software development teams, available on your schedule and configured to achieve success as defined by your requirements independently or in co-development with your team. Intertech teams combine proven full-stack, DevOps, Agile-experienced lead consultants with Delivery Management, User Experience, Software Development, and QA experts in Business Process Automation (BPA), Microservices, Client- and Server-Side Web Frameworks of multiple technologies, Custom Portal and Dashboard development, Cloud Integration and Migration (Azure and AWS), and so much more. Each Intertech employee leads with the soft skills necessary to explain complex concepts to stakeholders and team members alike and makes your business more efficient, your data more valuable, and your team better. In addition, Intertech is a trusted partner of more than 4000 satisfied customers and has a 99.70% “would recommend” rating.