Home > Backend Development > C++ > How to Combine Logical Expressions of Type `Expression` in LINQ?

How to Combine Logical Expressions of Type `Expression` in LINQ?

DDD
Release: 2025-01-31 06:01:09
Original
586 people have browsed it

How to Combine Logical Expressions of Type `Expression` in LINQ?

In the Linq, combine the logical expression of the type

Expression<Func<bool>> LINQ often uses expression, the expression is a representation of Lambda expression. When processing expressions, two logical expressions are often required, such as AND, OR, or Not.

For example, consider the following two expressions:

We may want to combine these expressions to obtain the same type of new expression:

Expression<Func<bool>> expr1;
Expression<Func<bool>> expr2;
Copy after login

<合> The expression of the combination with the same parameters

// 示例用法(原样无效)
Expression<Func<bool>> andExpression = expr1 AND expr2;
Copy after login

If <<> and use the same parameters, the operation is very simple:

<合> The expression of different parameters with a combination expr1 expr2

However, if the expression uses different parameters, we must pay attention to how to combine them. Use <现> to achieve this:
var body = Expression.AndAlso(expr1.Body, expr2.Body);
var lambda = Expression.Lambda<Func<bool>>(body, expr1.Parameters[0]);
Copy after login

<用> General method

The following code fragment provides a common method to determine the most suitable way to determine the combination expression: Invoke

static Expression<Func<bool>> AndAlso<T>(
    this Expression<Func<bool>> left,
    Expression<Func<bool>> right)
{
    var param = Expression.Parameter(typeof(T), "x");
    var body = Expression.AndAlso(
            Expression.Invoke(left, param),
            Expression.Invoke(right, param)
        );
    var lambda = Expression.Lambda<Func<bool>>(body, param);
    return lambda;
}
Copy after login
<注意> Other precautions

In .NET 4.0 and higher versions, you can use

Class to create EF security expressions:

static Expression<Func<bool>> AndAlso<T>(
    this Expression<Func<bool>> expr1,
    Expression<Func<bool>> expr2)
{
    ParameterExpression param = expr1.Parameters[0];
    if (ReferenceEquals(param, expr2.Parameters[0]))
    {
        return Expression.Lambda<Func<bool>>(
            Expression.AndAlso(expr1.Body, expr2.Body), param);
    }
    return Expression.Lambda<Func<bool>>(
        Expression.AndAlso(
            expr1.Body,
            Expression.Invoke(expr2, param)), param);
}
Copy after login
This Revied Output Maintains The Original Image and Provides a More Concise and Readable Explanation of the Code Snippets. TED for Better Readability.

The above is the detailed content of How to Combine Logical Expressions of Type `Expression` in LINQ?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template