Skip to main content

C# to JS: LINQ Expressions and ExpressionVisitor

How to translate (some) C# to JavaScript

If you’ve ever used something like EF Core you may have noticed that methods like LINQ’s Where take an Expression<Func<...>> as the first argument, instead of simply a Func<...>:

'Where' method definitions
Expressions were introduced in .NET 3.5 along with the rest of LINQ and they represent the code tree that a lambda is composed of. For example, if you had a lambda like () => a + b, this is how the resulting expression tree would look like:

Tree chart
.NET 4.0 later introduced ExpressionVisitor, which lets you traverse the expression tree. Take this visitor for example:

public class ConsolePrinterVisitor : ExpressionVisitor
{
    //This is just a helper method to get the Expression, normally you would already have it
    //so you'd just do ConsolePrinterVisitor.Visit(expression)
    public void WriteLambda(Expression<Func<object>> expression)
    {
        this.Visit(expression);
    }

    protected override Expression VisitBinary(BinaryExpression node)
    {
        Console.Write("(");
        base.Visit(node.Left);
        Console.Write($" {GetOperationChar(node.NodeType)} ");
        base.Visit(node.Right);
        Console.Write(")");

        return node;
    }

    protected override Expression VisitMember(MemberExpression node)
    {
        Console.Write(node.Member.Name);
        return node;
    }

    private static string GetOperationChar(ExpressionType type)
    {
        switch (type)
        {
            case ExpressionType.Add:
                return "+";
            case ExpressionType.Subtract:
                return "-";
            //Add other operations
        }

        return null;
    }
}

Usage:

int a = 1, b = 2, c = 3;

new ConsolePrinterVisitor().WriteLambda(() => a + b - c);
//Output: ((a + b) - c)

new ConsolePrinterVisitor().WriteLambda(() => a + (b - c));
//Output: (a + (b - c))

I’m sure you can already tell where JavaScript comes into all of this. By «simply» extending the visitor class to include method calls, field accesses and other language features you can successfully translate a C# lambda expression to a JS one. This is exactly what this class in my vue-aspvalidate library does in order to reuse a validator lambda in the ASP.NET Core back-end and in the JS/TS front-end! Here’s how that class is used:

Expression<Func<string, bool>> expr = o => o.Length > 4 && Regex.IsMatch(o, "^foo\\w+$", RegexOptions.Multiline | RegexOptions.IgnoreCase);

var js = expr.ToJs();

Console.WriteLine(js);
//Output: o=>o.length>4&&new RegExp("^foo\\w+$","gim").test(o)
//Beautified: o => o.length > 4 && new RegExp("^foo\\w+$", "gim").test(o)

For completeness’ sake, this is how the expression used in this example looks like in tree form:

Tree chart

Reading expressions is a powerful tool to have under your belt, but the real fun part comes with creating expression trees at runtime. This way you can dynamically generate an expression tree at runtime, compile it and run it at the exact same speed as normal, compiled code! This will come in the next post, so make sure to subscribe so you don’t miss it.

Comments

Popular posts from this blog

Hangfire: setup and usage

How to setup Hangfire, a job scheduler for .NET Installing Hangfire Setting up Hangfire Setting up storage MySQL Storage In-memory storage Running jobs Fire-and-forget jobs Delayed jobs Continuation jobs Recurring jobs ASP.NET Core job integration Conclusion Hangfire is a job scheduler for .NET and .NET Core that lets you run jobs in the background. It features various different job types: Fire-and-forget jobs are executed only once , shortly after creation. Delayed jobs are very similar to fire-and-forget jobs but they wait for a specified amount of time before running. Continuation jobs, which run after their parent job has finished . Recurring jobs, perhaps the most interesting of all, run repeatedly on an interval . If you like Unix’s cron you’ll feel right at home, as this kind of job lets you specify the job interval as a cron expression . If you have never used this I recommend crontab.guru , it has a live expression editor, as well as some examp...

ConditionalWeakTable, what does it do?

C#'s ConditionalWeakTable, what does it do? C# has many lesser known features, some more useful than others, and one of them is the ConditionalWeakTable<TKey, TValue> (keep in mind that TKey and TValue must be reference types). You can think of this type as a dictionary where the keys are weakly referenced, meaning that they won’t count when the GC checks if the object has to be collected. Additionally, when the keys do eventually get collected by the GC, that entry will get removed from the dictionary. This means that you can attach arbitrary objects to any object, allowing you to do something like this: public static class Extensions { private static ConditionalWeakTable < object , dynamic > Table = new ConditionalWeakTable < object , dynamic > ( ) ; public static dynamic Data ( this object obj ) { if ( ! Table . TryGetValue ( obj , out var dyn ) ) Table . Add ( obj , dyn = new ExpandoObject ( )...

How to observe and control Windows media sessions in C#

How to control Windows media sessions in C# If you use Windows 10 and play music, you’ll most likely come across this dialog at some point: This is the media overlay, and it appears whenever you’re playing music and press a media key or you change the volume. This dialog uses the GlobalSystemMediaTransportControls family of APIs available on the Windows 10 Runtime API since version 1809. This API is usually restricted to UWP applications, however, the introduction of .NET 5 and the ability to target specific platforms in the project’s TargetFramework has made it available for use in any kind of application, like WPF or regular console programs. Fetching information from the GSMTC API The API’s entrypoint is the GlobalSystemMediaTransportControlsSessionManager . This class allows you to fetch all the current active sessions, as well as whichever one Windows thinks is the current one. In order to be able to access this class at all you must first set your project’s TargetFramewor...