Developer knowledge network · moderated exchange

Супольнасць UnreliableCode

Супольнасць распрацоўшчыкаў даследаванняў, зваротнага праектавання і кадавання

Knowledge indexжыць
4Categories
919Threads
2.8KПаведамленні
Discussion

Building Dynamic LINQ Queries at runtime using System.Linq.Expressions.Expression Trees [StackOverflow Architecture Guide]

generic_math_geek
C# 11 Math Guru
MEMBER
прадстаўнік: 138
Дата далучэння: Jul 2021
Паведамленні: 10
Дзякуй: 88
1 месяцаў таму · Jul 18, 2026 2:36 AM
#1

How to construct dynamic SQL/database filters in C# without string concatenation:

CSHARP
// Building: p => p.Price > 100 && p.Category == "Electronics"
var parameter = Expression.Parameter(typeof(Product), "p");
var priceProp = Expression.Property(parameter, "Price");
var priceVal = Expression.Constant(100m);
var priceComparison = Expression.GreaterThan(priceProp, priceVal);

var lambda = Expression.Lambda<Func<Product, bool>>(priceComparison, parameter);
var filtered = dbContext.Products.Where(lambda).ToList();

Expression trees allow constructing type-safe queries that Entity Framework Core translates directly into parameterized SQL queries!

roslyn_source_gen
Roslyn Compiler Dev
MEMBER
прадстаўнік: 120
Дата далучэння: Feb 2020
Паведамленні: 12
Дзякуй: 75
1 месяцаў таму · Jul 18, 2026 4:25 AM
#2

Compiling expression trees (lambda.Compile()) creates native MSIL delegates at runtime, making it faster than standard reflection for dynamic object mapping.

dotnet_runtime_architect
.NET Core Specialist
MEMBER
прадстаўнік: 103
Дата далучэння: Apr 2018
Паведамленні: 40
Дзякуй: 24
1 месяцаў таму · Jul 18, 2026 4:33 PM
#3

Essential knowledge for building dynamic search filters and ORM mappers.