You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过循环动态构建多Contains或条件的Lambda表达式树

Dynamic Lambda Expression Tree for Multiple OR Contains Checks

Got it, let's ditch those repetitive if-else blocks and build that OR chain dynamically. Here's how you can do it step by step:

Core Idea

We'll use Expression.OrElse to chain together multiple Contains call expressions. Start with the first Contains check, then iterate through the rest of your values, merging each new Contains into the existing expression with OR logic.

Step-by-Step Implementation

  1. Create the parameter expression: This represents the x in your lambda (x => ...).
  2. Get the property access expression: Target the fonction property on the Contacts type.
  3. Build individual Contains expressions: For each value in your list, create an expression that calls string.Contains on x.fonction with the current value.
  4. Chain expressions with OrElse: Combine all the individual Contains expressions into a single expression using OR logic. If there's only one value, we just use that single expression.
  5. Assemble the final Lambda: Wrap the parameter and the combined expression into an Expression<Func<Contacts, bool>>.

Full Code Example

List<string> values = new List<string> { "value1", "value2", "value3", "value4", "value5" };

// 1. Define the parameter for the lambda (x)
ParameterExpression param = Expression.Parameter(typeof(Contacts), "x");

// 2. Get the access to x.fonction
MemberExpression fonctionProp = Expression.Property(param, nameof(Contacts.fonction));

Expression? combinedExpr = null;

foreach (var value in values)
{
    // 3. Create a constant expression for the current value
    ConstantExpression valueConst = Expression.Constant(value, typeof(string));
    
    // 3. Build the Contains call: x.fonction.Contains(value)
    MethodCallExpression containsCall = Expression.Call(
        fonctionProp,
        typeof(string).GetMethod(nameof(string.Contains), new[] { typeof(string) })!,
        valueConst
    );
    
    // 4. Chain with OrElse
    if (combinedExpr == null)
    {
        combinedExpr = containsCall;
    }
    else
    {
        combinedExpr = Expression.OrElse(combinedExpr, containsCall);
    }
}

// Fallback in case values is empty (adjust this based on your needs)
if (combinedExpr == null)
{
    // Example: return false if no values, or you could return true
    combinedExpr = Expression.Constant(false);
}

// 5. Build the final lambda expression
Expression<Func<Contacts, bool>> expr1 = Expression.Lambda<Func<Contacts, bool>>(combinedExpr, param);

Notes

  • Make sure to handle the empty values case (we added a fallback here, but you can adjust it to fit your business logic—like returning true if there are no filters, or throwing an exception).
  • The GetMethod call uses nameof to avoid magic strings, which makes the code more maintainable.
  • This approach works for any number of values, so you don't have to update code when the list grows or shrinks.

内容的提问来源于stack exchange,提问作者Bob5421

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:39:53