Moonglade

Modern Markdown blogging with AI-powered localization and search.

Tag: expression trees


What is the difference between Linq and lambda?

This article aims to clarify the concepts of LINQ and Lambda expressions, which are often confused, by pointing out that the two are fundamentally different. LINQ, or Language Integrated Query, is essentially a collection of extension methods; it can be used with either query expression syntax or method syntax. Lambda expressions, on the other hand, are a form of syntactic sugar designed to simplify the creation of anonymous delegates, where the arrow symbol represents the return operation. In method syntax, Lambda expressions are commonly used as parameters to construct `Func` delegates, but developers can also use named methods as an alternative, with both approaches being functionally equivalent. An advanced use of Lambda expressions is their ability to build expression trees, representing code as data. This enables LINQ to convert C# query statements into other forms, such as SQL, which is also the underlying principle behind many ORM frameworks. The article emphasizes that although the two concepts often appear together in documentation, developers should master the fundamental principles and avoid blindly chasing new technologies at the expense of core knowledge. By distinguishing between the methods of creating delegates and the nature of query libraries, developers can clearly understand the differences between the two.

Views 173
C# Method Syntax entity relationship mapping expression trees lambda expressions linq

Simplifying DDD Value Object Comparison with EF Core Dynamic Expression Trees

This article explores technical solutions for implementing semantic equality queries with value objects in EF Core. It first points out that traditional field-by-field query code is verbose and lacks semantic clarity, while directly comparing value objects for equality results in a compilation exception. To address this, the article proposes and implements a generic `ValueObjectEqualHelper` static class, whose core is the `CheckEqual` method. This method dynamically builds expression trees, iterates through the value object's properties, automatically skips null values to avoid generating redundant `IS NULL` conditions, and correctly constructs binary expression trees for equality comparisons based on property types (primitive or reference types), finally connecting all conditions with `AndAlso` logic. This approach preserves the intuitive semantics of `p => p.Address == new Address(...)` while being correctly translated into SQL by EF Core, solving the pain point of being unable to directly compare value objects for equality in database queries and providing a generic and elegant implementation path.

Views 168
.NET C# Dynamic Condition Generation EF Core Entity Framework Core expression trees

Building C# Expression Trees Step by Step

This article uses the construction of the common Lambda expression `p => p.Age > 18` as an example to demonstrate in detail the entire process of manually building an expression tree in C#. The article first analyzes the correspondence between the elements of the expression and the tree nodes, then step-by-step shows how to sequentially create parameter expressions, member expressions, constant expressions, and binary operator expressions. Finally, it uses `Expression.Lambda` to combine these nodes into a complete Lambda expression. The text also includes actual code for compiling the expression and using it for list filtering. Addressing the question of why manual compilation might seem redundant, the article points out that its core value lies in its use with `IQueryable` and EF Core, enabling dynamic queries without the need for manual compilation. This technique can be used to simplify dynamic comparison of value objects in DDD, or to set global filters for entity properties (such as soft delete), thereby enhancing code flexibility and maintainability.

Views 121
.NET C# Dynamic Expression EF Core Linq Expressions expression trees

How to use C# expression trees?

Expression trees are a technique that uses tree-based data structures to represent the logic of code operations. In .NET, they correspond to the `Expression<TDelegate>` type. They allow code to be manipulated as data, and can be compiled into executable delegate objects via the `Compile` method. This article demonstrates, through a hands-on example of manually constructing an expression tree, how to replace hardcoded property name validation logic. The specific steps include: first, creating a `ParameterExpression` as the parameter node; next, using `Expression.PropertyOrField` or `MakeMemberAccess` to build member access nodes; and finally, assembling a complete Lambda expression via `Expression.Lambda` and invoking it using `DynamicInvoke`. This approach effectively resolves maintenance challenges caused by property name changes, enhancing code flexibility and maintainability. Although dynamically constructing expression trees involves relatively complex and less intuitive code, its advantages are significant in scenarios requiring the avoidance of hardcoding and the implementation of runtime dynamic logic. It serves as an effective means of simplifying complex query operations, such as comparing value objects.

Views 106
C# Dynamic Code Generation Functional Programming Property Access expression trees lambda expressions