Last Updated on October 29, 2025 by Aram
In this article you will learn about 2 great and powerful libraries for dynamic processing of LINQ queries as well as executing, compiling, and running C# code at runtime.
These libraries are the Dynamic LINQ and C# Eval Expression Libraries.
The article will deep dive into each of these libraries, with an introduction, use cases, how to use, and some practical examples that help in better exploring their capabilities.
Thanks for ZZZ Projects for bringing these fantastic libraries into the world of .NET and C#, and for collaborating and sponsoring this article.
This brilliant team has been putting massive efforts to build highly productive and powerful tools to simplify the lives of the developers and thus improve the quality of products.
So, let’s get started with learning about the Dynamic LINQ and C# Eval Expression.
Dynamic LINQ
Ever wondered how you can build dynamic filters in LINQ without writing endless if statements?
That’s where Dynamic LINQ comes in.
Dynamic LINQ is a Free and Open-Source library.
Downloaded over 254 Million times, this library adds powerful extensions to standard LINQ.
It lets you build LINQ queries at runtime using string expressions instead of code.
Dynamic LINQ helps you build runtime expressions and execute LINQ queries at runtime.
How to Use Dynamic LINQ?
To use Dynamic LINQ, you need to install the free NuGet Package:
System.Linq.Dynamic.Core

And then you can start using it easily as part your regular LINQ code with a few tweaks:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System.Linq.Dynamic.Core; List<Product> products = [ new() { Name = "Laptop", Category = "Electronics", Price = 1200 }, new() { Name = "Phone", Category = "Electronics", Price = 800 }, new() { Name = "Desk", Category = "Furniture", Price = 300 }, ]; // Dynamic filter, sort, and projection var result = products .AsQueryable() .Where("Category == @0 && Price >= @1", "Electronics", 900) .OrderBy("Price desc") .Select("new (Name, Price)") .ToDynamicList(); foreach (dynamic item in result) Console.WriteLine($"{item.Name} - {item.Price}"); // Output: Laptop - 1200 |
You can try it yourself here using this DotNetFiddle.
DotNetFiddle is an amazing tool that allows you to run your .NET code online over any modern browser, this tool is also brought to you by ZZZ Projects.
Features of Dynamic LINQ
Below are some key features of Dynamic LINQ:
String-Based Queries
Write queries as strings, such as .Where("Age > 30"), allowing dynamic construction of queries.
The queries are written using the Expression Language, which is designed to be familiar with VB, C#, and SQL users.
Here is a sample code that shows how you can use the Expression Language to filter and sort a queryable collection:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; List<Product> products = [ new() { Name = "Laptop", Category = "Electronics", Price = 1200 }, new() { Name = "Phone", Category = "Electronics", Price = 800 }, new() { Name = "Desk", Category = "Furniture", Price = 300 }, ]; // Dynamic filter, sort, and projection var result = products .AsQueryable() .Where("Category == @0 && Price >= @1", "Electronics", 900) .OrderBy("Price desc") .Select("new (Name, Price)") .ToDynamicList(); foreach (dynamic item in result) Console.WriteLine($"{item.Name} - {item.Price}"); public class Product { public string Name { get; set; } public string Category { get; set; } public decimal Price { get; set; } } // Output: Laptop - 1200 |
You can read further about the Expression Language in the official docs here.
Expression Parsing
Utilize methods like ParseLambda and Parse to convert string expressions into executable LINQ expressions.
Here is an example for how to use ParseLambda from the DynamicExpressionParser Class:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; using System.Linq.Dynamic.Core; using System.Linq; using System.Collections.Generic; // Setup a list of objects to query var customers = new List<Customer> { new Customer { City = "London", Orders = { "A" }, CompanyName = "Alpha" }, new Customer { City = "London", Orders = { "A" }, CompanyName = "TestCo" }, new Customer { City = "Paris", Orders = { "B" }, CompanyName = "Beta" }, }.AsQueryable(); var e1 = DynamicExpressionParser.ParseLambda<Customer, bool>( new ParsingConfig(), true, "City = @0", "London" ); var e2 = DynamicExpressionParser.ParseLambda<Customer, bool>( new ParsingConfig(), true, "c => c.CompanyName != \"TestCo\"" ); var filteredCustomers = customers.Where("@0(it) and @1(it)", e1, e2).ToList(); Console.WriteLine($"Found {filteredCustomers.Count} customer(s)."); Console.WriteLine($"Result: {filteredCustomers.First().CompanyName} in {filteredCustomers.First().City}"); public class Customer { public string City { get; set; } public string CompanyName { get; set; } public List<string> Orders { get; set; } = new List<string>(); } // Output: // Found 1 customer(s). // Result: Alpha in London |
You can try it out yourself here
Dynamic Class Creation
Generate new data classes at runtime using CreateClass, facilitating scenarios where the structure of data is not known at compile time.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; using System.Linq.Dynamic.Core; var properties = new[] { new DynamicProperty("Name", typeof(string)), new DynamicProperty("Birthday", typeof(DateTime)) }; Type type = DynamicClassFactory.CreateType(properties); var instance = Activator.CreateInstance(type); type.GetProperty("Name").SetValue(instance, "Mozart"); type.GetProperty("Birthday").SetValue(instance, new DateTime(1756, 1, 27)); Console.WriteLine(instance); // Output: // { Name = Mozart, Birthday = 01/27/1756 00:00:00 } |
Dynamic Class creation can be useful in Dynamic Data Shaping in RESTful APIs where the frontend can select which fields to return and the backend would dynamically generate the class from that selection.
Building dynamic reports, dashboards, query building from dynamic UI are all practical use cases for dynamic class creation.
Null Propagation
Handle null values gracefully with functions like np(), enabling safe navigation through potentially null properties.
With dynamic queries it is essential to handle null safely, because the property path may not always exist.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; using System.Linq; using System.Collections.Generic; using System.Linq.Dynamic.Core; var people = new List<Person> { new Person { Name = "Alice", Address = new Address { City = "London" } }, new Person { Name = "Bob", Address = null } }; // Use np() to safely access nested property var cities = people.AsQueryable() .Select("np(Address.City)") .ToDynamicList(); foreach (var city in cities) Console.WriteLine(city ?? "unknown"); public class Address { public string Street { get; set; } public string City { get; set; } public string Country { get; set; } } public class Person { public string Name { get; set; } public int Age { get; set; } public Address Address { get; set; } } // Output: // London // unknown |
Here is a live example for you to play around.
It makes string-based LINQ expressions safer and more reliable in runtime evaluation.
Entity Framework Integration
Seamlessly integrate with Entity Framework, supporting operations like Like queries and asynchronous methods.
Take a look at the below code, and feel free to explore it further in this DotNetFiddle.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using System.Linq.Dynamic.Core; using var context = new AppDbContext(); // Ensure database is created await context.Database.EnsureCreatedAsync(); if (!context.Orders.Any()) { var orders = Enumerable.Range(1, 10).Select(i => new Order { Description = $"Order {i}", Amount = 10.5m * i, IsProcessed = i % 2 == 0 }).ToList(); foreach (var order in orders) { context.Orders.Add(order); } await context.SaveChangesAsync(); var filtered = await context.Orders .Where("IsProcessed == @0", true) .OrderBy("Amount desc") .Select("new(Description, IsProcessed, Amount)") .ToDynamicListAsync(); foreach (var c in filtered) { Console.WriteLine($"{c.Description} | {c.IsProcessed} | {c.Amount}"); } } public class Order { public int Id { get; set; } public string Description { get; set; } public bool IsProcessed { get; set; } public decimal Amount { get; set; } } public class AppDbContext : DbContext { public DbSet<Order> Orders { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer())); base.OnConfiguring(optionsBuilder); } } // Output: // Order 10 | True | 105.00 // Order 8 | True | 84.00 // Order 6 | True | 63.00 // Order 4 | True | 42.00 // Order 2 | True | 21.00 |
See? It is as simple as using the regular LINQ with Entity Framework Core.
Use Cases of Dynamic LINQ
Dynamic LINQ has several use cases, we have just seen quite a few in the previous examples.
Here is a complete list of them for your reference, so that you can relate to any of your areas you are currently working in:
User-Driven Filtering & Sorting
Build flexible queries from user input for search, sort, and filtering across UI or APIs.
Reporting & Analytics
Generate dynamic reports, exports, and projections without changing backend code.
Administration & Operations
Empower admins to query, clean, or manage data through configurable runtime filters.
Multi-Tenant & Rule-Based Logic
Apply tenant-specific or configuration-based filtering rules dynamically.
Development & Testing Utilities
Run quick ad-hoc queries for debugging, QA, or validating data with minimal setup.
C# Eval Expression
Did you know that you can execute dynamic C# code at runtime, just like JavaScript’s eval(), using the C# Eval Expression Library?
C# Eval Expression lets you evaluate expressions, execute statements, or even compile and run LINQ queries dynamically, all without precompiling code.
It is a powerful library that allows you to:
- Execute C# code as a string at runtime.
- Dynamically create and run logic based on user input or configuration.
- Avoid reflection-heavy or switch-case-heavy designs.
C# Eval Expression Use Cases
Here are the use cases for C# Eval Expression:
Dynamic Filtering
Build flexible search queries based on user-selected filters.
Formula Engines
Evaluate user-defined calculations or business rules.
Config-driven Logic
Run C# expressions stored in database configurations.
Dynamic Mapping
Transform data with expressions defined at runtime.
How to use C# Eval Expression?
To use C# Eval Expression, you first need to install Z.Expressions.Eval Nuget package.

And then you can start using any of its methods in your code.
C# Eval Expression Main Methods
C# Eval Expression provides a number of methods that could be used to evaluate, execute, and compile expressions and C# code at runtime
Execute
The execute method is powerful and flexible that you can virtually run any expression in it, simple or complicated.
Simple like:
|
1 2 3 4 5 6 7 |
using Z.Expressions; var result = Eval.Execute<int>("int x = 5; x * 2;"); Console.WriteLine(result); // Output: 10 |
Or multi-line, and performs a more complex mathematical operation:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using Z.Expressions; string formula = @" (BasePrice * Math.Pow(1 + InterestRate, Years)) + (ExtraFees * (1 + TaxRate)) "; var parameters = new { BasePrice = 10000.0, InterestRate = 0.05, Years = 3, ExtraFees = 250, TaxRate = 0.16 }; double totalAmount = Eval.Execute<double>(formula, parameters); Console.WriteLine($"Total Calculated Amount: {totalAmount:F2}"); // Output: Total Calculated Amount: 11866.25 |
You can test the same code and see the output yourself in this DotNetFiddle
Compile
The compile part refers to the process of parsing and converting a C# expression string into a compiled, executable delegate, similar to how normal C# code is compiled, but done at runtime.
This is useful whenever you want to pre-compile a formula and then call it multiple times at runtime
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using Z.Expressions; string expression = "(x + y) * factor"; var compiled = Eval.Compile<Func<double, double, double, double>>( expression, "x", "y", "factor" ); double result1 = compiled(100, 50, 2); double result2 = compiled(200, 25, 3); Console.WriteLine($"Result 1: {result1:F2}"); Console.WriteLine($"Result 2: {result2:F2}"); // Output: // Result 1: 300.00 // Result 2: 675.00 |
Here is the live fiddle of the above code to test it yourself.
So the steps are simple:
- Define a formula as a string
- Compile the formula into a reusable function (Func<….>), which is a typed function that you can call multiple times
- Execute the compiled function with inputs. Each call calculates the result for the given parameters quickly.
- Results returned into variables where you can handle them at your own convenience.
So the main idea is that you define the formula once as a string, compile it, and then reuse it efficiently for multiple calculations without re-parsing the string each time.
Run Dynamic LINQ
The same features of the Dynamic LINQ library is built-into the C# Eval Expression library but with a different flavor and more control.
Check the code sample below how you can use dynamic lambda expressions in different way to filter and sort a collection:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; using System.Linq; var products = new[] { new { Name = "Laptop", Price = 1200, Category = "Electronics" }, new { Name = "Shirt", Price = 25, Category = "Apparel" }, new { Name = "Coffee Maker", Price = 85, Category = "Home Appliances" } }; // Use Eval to filter dynamically var filteredProducts = products .AsQueryable() .WhereDynamic("p => p.Price > 50") .OrderByDynamic(p => "p.Name") .ToList(); foreach (var product in filteredProducts) { Console.WriteLine($"Name: {product.Name}, Price: {product.Price}, Category: {product.Category}"); } // Output: // Name: Coffee Maker, Price: 85, Category: Home Appliances // Name: Laptop, Price: 1200, Category: Electronics |
And you can fiddle around with this code here.
Dynamic LINQ inside C# Eval Expression is essentially runtime LINQ with string-based expressions, giving you powerful flexibility for filtering, sorting, and projecting data dynamically.
C# Eval Expression EvalContext
EvalContext is a core feature and the main class in the C# Eval Expression library.
It gives you control and isolation when executing dynamic C# code.
You can have multiple EvalContext instances, and each one would represent its own runtime container, that includes its own variables, methods, namespaces, references, and compilation settings.
There are 2 ways that you can use EvalContext:
Global Context
The global context is the default static context used behind the scenes whenever you call the Eval class directly.
So whenever we call:
|
1 |
var result = Eval.Execute<int>("1 + 2"); |
Behind the scenes, Eval is referring to the Global Instance of EvalContext accessed via the EvalManager.
So this the above call is equivalent to the below statement:
|
1 |
var result = EvalManager.DefaultContext.Execute<int>("1 + 2"); |
Instance Context
This is where you can create your own instance of the EvalContext and define your own parameters in it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using Z.Expressions; var context = new EvalContext(); context.RegisterLocalVariable("discount", 0.10); var result = context.Execute<double>( "price * (1 - discount)", new { price = 100.0 } ); Console.WriteLine($"Final Price: {result}"); // Output: 90.0 |
You can run this live here.
C# Eval Expression is your ultimate tool to execute, compile, and run your C# code and dynamic queries.
You can download the free trial of C# Eval Expression, and you can extend it its validity for a few months by installing the latest version at the beginning of every month.
If you are a developer, architect, or in a team who need dynamic expression evaluation, runtime formula execution, or configurable business rules in production applications, you should purchase the C# Eval Expression license to safely and efficiently integrate its amazing capabilities.
Always remember that all LINQ-related dynamic extensions are completely free, even in C# Eval Expression library.
Eval and Compile methods are free as well for expressions up to 50 characters. You can remove this limitation by purchasing a license. And you are still able to test longer characters while in trial mode.
You can learn more here.
Final Thoughts
Dynamic LINQ and C# Eval Expressions both let you evaluate logic at runtime, but they serve slightly different purposes.
- Dynamic LINQ is great for building queryable expressions dynamically, especially when filtering or sorting data in databases or collections based on user input.
- C# Eval Expressions excels at executing arbitrary C# expressions with full math, functions, and logic at runtime, ideal for configurable formulas, pricing engines, or business rules.
Together, they give you the flexibility to adapt your code dynamically without redeploying, while keeping your core application clean and maintainable.
Choosing between them depends on whether you only need query flexibility (Dynamic LINQ) or full comprehensive expression evaluation for your C# code at runtime (C# Eval Expression).
This article only shows the tip of the iceberg for both Dynamic LINQ and C# Eval Expression libraries.
There is a lot more to be explored on these wonderful tools.
The references section include links to get started for both libraries and there you can navigate through the documentation and try different methods and implementations yourself.
References
C# Eval Expression Getting Started
Recent Articles
Exploring Bulk Operations in EF Core
7 Types of Authorization in ASP.NET Core Web API
Structured Logging in ASP.NET Core
Options Pattern in ASP.NET Core
Bonus
Here is a brilliant piece of music dedicated for this article:
Händel: Water Music Suite No. 3 in G major, HWV 350 (with Score)
Performed by Les Violons du Roy Orchestra
