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

扩展方法if条件实现:仅纳入值为Match的参数

Solution for Conditional Logic with "Match" Parameters

Got it, let's work through this problem together. From your description, you're building an extension method where you only want to add a TreeDocument to the list if certain parameters are set to Match—and you need clean conditional logic that only accounts for those Match values, ignoring No Match or Empty.

First, let's clarify the core idea: For each of your four string parameters, we only want to include its corresponding business check in the if condition if the parameter equals "Match". If it's "No Match" or "Empty", we skip that check entirely.

Approach 1: Straightforward Conditional Checks

This is simple and easy to read, perfect for small numbers of parameters:

public static void AddIfNotNullAndInAdvancedSearch(
    this List<TreeDocument> docs, 
    TreeDocument value, 
    string studyTitleContainsSearch, 
    string authorMatch, 
    string dateMatch, 
    string tagMatch)
{
    // First handle the "NotNull" part of the method name
    if (value == null) return;

    bool shouldAdd = true;

    // Only check the study title condition if the parameter is "Match"
    if (studyTitleContainsSearch == "Match" && !value.StudyTitle.Contains(/* Your search term here */))
    {
        shouldAdd = false;
    }

    // Repeat for other parameters
    if (authorMatch == "Match" && !value.Author.Contains(/* Author search term */))
    {
        shouldAdd = false;
    }

    if (dateMatch == "Match" && !(value.Date >= /* Start date */ && value.Date <= /* End date */))
    {
        shouldAdd = false;
    }

    if (tagMatch == "Match" && !value.Tags.Contains(/* Target tag */))
    {
        shouldAdd = false;
    }

    if (shouldAdd)
    {
        docs.Add(value);
    }
}

Approach 2: Clean, Scalable Condition Collection

If you might add more parameters later, this approach keeps your code organized and easy to extend:

public static void AddIfNotNullAndInAdvancedSearch(
    this List<TreeDocument> docs, 
    TreeDocument value, 
    string studyTitleContainsSearch, 
    string authorMatch, 
    string dateMatch, 
    string tagMatch)
{
    if (value == null) return;

    var requiredConditions = new List<Func<bool>>();

    // Add condition only if parameter is "Match"
    if (studyTitleContainsSearch == "Match")
    {
        requiredConditions.Add(() => value.StudyTitle.Contains(/* Search term */));
    }

    if (authorMatch == "Match")
    {
        requiredConditions.Add(() => value.Author.Contains(/* Author term */));
    }

    if (dateMatch == "Match")
    {
        requiredConditions.Add(() => value.Date >= /* Start date */ && value.Date <= /* End date */);
    }

    if (tagMatch == "Match")
    {
        requiredConditions.Add(() => value.Tags.Contains(/* Target tag */));
    }

    // All conditions must pass (or pass automatically if there are no conditions)
    bool shouldAdd = requiredConditions.All(condition => condition());

    if (shouldAdd)
    {
        docs.Add(value);
    }
}

Pro Tip: Replace Magic Strings with an Enum

To avoid typos and make your code more type-safe, replace those string parameters with an enum. This eliminates invalid values and makes your code clearer:

public enum MatchStatus
{
    Match,
    NoMatch,
    Empty
}

public static void AddIfNotNullAndInAdvancedSearch(
    this List<TreeDocument> docs, 
    TreeDocument value, 
    MatchStatus studyTitleStatus, 
    MatchStatus authorStatus, 
    MatchStatus dateStatus, 
    MatchStatus tagStatus)
{
    if (value == null) return;

    var requiredConditions = new List<Func<bool>>();

    if (studyTitleStatus == MatchStatus.Match)
    {
        requiredConditions.Add(() => value.StudyTitle.Contains(/* Search term */));
    }

    // If you ever need to handle "NoMatch" later, just add:
    // else if (studyTitleStatus == MatchStatus.NoMatch)
    // {
    //     requiredConditions.Add(() => !value.StudyTitle.Contains(/* Search term */));
    // }

    // Repeat for other parameters...

    bool shouldAdd = requiredConditions.All(c => c());
    if (shouldAdd) docs.Add(value);
}

All these approaches ensure that only parameters set to Match contribute to your conditional logic, exactly what you asked for.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:30:38