Moonglade

Modern Markdown blogging with AI-powered localization and search.

Tag: lambda expressions


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

Dynamically Building Expression Trees in EF Core to Set Soft Delete Query Filters for All Entities

This article introduces two QueryFilter configuration approaches for implementing soft delete in Entity Framework Core. First, it demonstrates the conventional method of manually adding `builder.HasQueryFilter(x => !x.SoftDelete)` in each entity configuration class. While simple and direct, this approach incurs high maintenance costs when dealing with a large number of entities. Subsequently, it proposes a superior dynamic expression tree construction scheme. By iterating through all entity types in the model, this approach automatically detects the presence of a `bool` type `SoftDelete` property and uses the `Expression` class to dynamically generate the `x => !x.SoftDelete` Lambda expression to uniformly set the query filter. This method eliminates code duplication and achieves global automation of soft delete filtering. The article also demonstrates how to use the `IgnoreQueryFilters()` method to ignore the filter when necessary to query deleted data, and provides various code implementations ranging from detailed construction to simplified syntax. Finally, it emphasizes the flexibility and vitality of C# syntax in constructing expression trees.

Views 136
.NET Dynamic Expression Trees EF Core Entity Framework Core Soft Delete lambda expressions

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