Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Compile Linq Expressions to increase performance

Compile Linq Expressions to increase performance
October 7, 2022 3 min read BEN ABT
.NET Csharp

This article was written in 2022. Versions, tools and recommendations may have changed since then. Newer posts are collected under .NET.

Expressions are now an absolute part of a developer’s everyday life in .NET thanks to Linq.

However, due to their nature, expressions are not one of the very best performing tools, which is why the .NET team is also doing a lot to improve general performance while maintaining usability.

Performance Improvements in .NET 7

In simpler words, expressions are a markup language for how the runtime should traverse a “tree” to get properties or perform comparisons. This traversal takes time. And the more often you do it, the more time it takes.

However, expressions can also be compiled so that the traversal only has to be done once and the runtime otherwise has a plan of what to do. Very similar to how database statements (plans) are also cached.
Afterwards you only have to use the plan and save the overhead - and it is really big!

Unfortunately, it is not currently possible to shift the expression entirely to compile-time, but at least the overhead can be shifted to a one-time operation. Not to mention that compiling itself also takes time.

Sample Code

As an example for my benchmark I have chosen a very simple expression, so that you can see that even with the simplest expressions the effect is present. However, the more complex the expression, the higher the effect!

So we see an expression that only performs an Int-based comparison to get an element.

C#
    _persons.Where( person => person.Age == 23).Single();

To perform a compilation the expression must first be taken out of the operation itself, for example as a static field.

C#
    private static readonly Expression<Func<Person, bool>> s_ageExpression = e => e.Age == 23;

This expression alone can actually be used.

C#
    public Person Ex() => _persons.Where(s_ageExpression).Single();

However, we wanted to compile our expression, for which we can again use a static field.

C#
1    private static readonly Expression<Func<Person, bool>> s_ageExpression = e => e.Age == 23;
2    private static readonly Func<Person, bool> s_ageExpressionCompiled = s_ageExpression.Compile();

Benchmark

So, as a result, we compare two things:

  • A Where-query with a simple expression and one with a compiled expression
C#
1    private static readonly Expression<Func<Person, bool>> s_ageExpression = e => e.Age == 23;
2    [Benchmark]
3    public Person Ex() => _persons.Where(s_ageExpression).Single();
4
5    // Compiled
6    private static readonly Func<Person, bool> s_ageExpressionCompiled = s_ageExpression.Compile();
7    [Benchmark(Baseline = true)]
8    public Person Ex_Compiled() => _persons.Where(s_ageExpressionCompiled).Single();

The result is very clear: the compiled query is over 3000 more performant.

Markdown
 1BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19044.2006/21H2/November2021Update)
 2AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
 3.NET SDK=7.0.100-rc.1.22431.12
 4  [Host]     : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT AVX2
 5  DefaultJob : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT AVX2
 6
 7
 8|      Method |          Mean |        Error |       StdDev |    Ratio |
 9|------------ |--------------:|-------------:|-------------:|---------:|
10|          Ex | 228,941.16 ns | 4,508.268 ns | 4,823.797 ns | 3,464.82 |
11| Ex_Compiled |      66.10 ns |     1.258 ns |     1.346 ns |     1.00 |

Full Sample and Results: Benjamin Abt - Sustainable Code - Compiled Expressions

Conclusion:

It can be very worthwhile to compile certain expressions, the effect and performance improvements are gigantic.

Related articles

Production Rate Limiting in ASP.NET Core

Nov 3, 2026 - 16 min read

Production Rate Limiting in ASP.NET Core

Most ASP.NET Core rate-limiting examples stop after registering a fixed window. That is enough to demonstrate the middleware, but it is not …

The Big Decimal Bugfix with .NET 11

Nov 2, 2026 - 11 min read

The Big Decimal Bugfix with .NET 11

For more than two decades, (decimal)1.23 returned 1.23m. It looked correct, it passed every test and it was a polite lie.

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026 - 10 min read

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled and Alpine are often compared as three equivalent Linux distributions. They are not. Alpine is a distribution built …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact