首页 > 解决方案 > 如果条件匹配特定条件,则分配 - c#

问题描述

关于以下逻辑的一个非常基本的问题。

如果该术语以双引号开头,我不想做任何事情。只想将它传递到下一行。但是该行term = term会引发错误。

不知道如何处理。

if (term.StartsWith("$"))
{
    // Don't modify the search term
    term = term.Substring(1, term.Length - 1);
    originalTerm = string.Copy(term);
}
else
{
    if (term.StartsWith("\""))
    {
        term = term;
    }
    else
    {
        // Escape special characters
        term = LuceneReservedCharacterPattern.Replace(term, @"\$0"); 
        // Then modify the term to improve phrase match:
        //      lyme disease OR "lyme disease"^2
        term = string.Concat("(", term, ") OR \"", term, "\"^2");
    }
    
    if (collections != null && !collections.Any(c => c.Equals("all", StringComparison.OrdinalIgnoreCase)))
    {
        // check for multiple collections collections: (all OR this OR that OR him OR her) etc
        term = string.Concat("(", term, ") AND collections:(", string.Join(" OR ", collections), ")");
    }
}

标签: c#

解决方案


只需反转条件并删除else..?这样,如果条件是true,则什么都不会发生。


推荐阅读