简体   繁体   English

可以用C#切换这些语句吗?

[英]Possible to C# switch these statements?

Possible to C# switch these statements? 可以用C#切换这些语句吗? I am not bright enough to see the possibility. 我不够聪明,看不到这种可能性。

        if (sortExpression.IndexOf("companyCode", StringComparison.OrdinalIgnoreCase) >= 0)
        {
            sortExpression += ", costCenter, acc_code";
        }
        if (sortExpression.IndexOf("costCenter", StringComparison.OrdinalIgnoreCase) >= 0)
        {
            sortExpression += ", companyCode, acc_code";
        }
        if (sortExpression.IndexOf("acc_code", StringComparison.OrdinalIgnoreCase) >= 0)
        {
            sortExpression += ", companyCode, costCenter";
        }
        {
            //default, everything else.
        }

You can certainly reduce the code repetition. 您当然可以减少代码重复。

Try this: 尝试这个:

var map = new Dictionary<string, string>()
{
    { "companyCode", ", costCenter, acc_code" },
    { "costCenter", ", companyCode, acc_code" },
    { "acc_code", ", companyCode, costCenter" },
};

var value =
    map
        .Where(x => sortExpression.IndexOf(x.Key, StringComparison.OrdinalIgnoreCase) >= 0)
        .Select(x => x.Value)
        .FirstOrDefault();

if (value != null)
{
    sortExpression += value;
}
else
{
    //default, everything else.
}

No, because you are testing different variables to a common value. 否,因为您要测试不同的变量是否具有相同的值。 Switch statements test a common variable to different values. switch语句将一个公共变量测试为不同的值。 To make a switch statement work in your case every if statement would have to be comparing the same variable. 为了使switch语句在您的情况下起作用,每个if语句都必须比较同一变量。
ie

    if (sortExpression.IndexOf("companyCode", StringComparison.OrdinalIgnoreCase) >= 1)
    {
        sortExpression += ", costCenter, acc_code";
    }
    if (sortExpression.IndexOf("companyCode", StringComparison.OrdinalIgnoreCase) >= 2)
    {
        sortExpression += ", companyCode, acc_code";
    }
    if (sortExpression.IndexOf("companyCode", StringComparison.OrdinalIgnoreCase) >= 3)
    {
        sortExpression += ", companyCode, costCenter";
    }
    {
        //default, everything else.
    }

As you can see all of the variables are comparing sortExpression.IndexOf("companyCode", StringComparison.OrdinalIgnoreCase) to different values, rather than the other way around. 如您所见,所有变量都将sortExpression.IndexOf("companyCode", StringComparison.OrdinalIgnoreCase)与不同的值进行比较,而不是相反。

I do not think this is doing what you want. 我不认为这是您想要的。
In first if you search for "companyCode" and add 2 other fields if it exists. 首先,如果您搜索“ companyCode”并添加其他2个字段(如果存在)。
So when first if is true, than other 2 ifs will be true as you will add other fields inside first if. 因此,当first if为true时,其他2个ifs为true,因为您将在first if中添加其他字段。

You could change this to "switch" if initially sortExpression would have single field. 如果最初的sortExpression具有单个字段,则可以将其更改为“ switch”。

switch (sortExpression)
{
    case "companyCode":
        sortExpression += ", costCenter, acc_code";
        break;
    case "costCenter":
        sortExpression += ", companyCode, acc_code";
        break;
    case "acc_code":
        sortExpression += ", companyCode, costCenter";
        break;
    default:
        //default, everything else.
        break;
}

If not you could use something like this: 如果没有,您可以使用类似以下的方法:

public string GetSortExpression(string sortExpression)
{
    List<string> requiredFields = new List<string>(new[] { "companyCode", "costCenter", "acc_code" });
    if (string.IsNullOrEmpty(sortExpression) // string is null or empty
        || !requiredFields.Any(x => sortExpression.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0)) // string does not contain any of fields
    {
        sortExpression = "default, everything else";
    }
    else
    {
        List<string> fields =
        sortExpression.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
        fields.AddRange(requiredFields.Except(fields));
        sortExpression = string.Join(", ", fields);
    }
    return sortExpression;
}

Testing: 测试:

[TestCase(null, "default, everything else")]
[TestCase("", "default, everything else")]
[TestCase("companyCode", "companyCode, costCenter, acc_code")]
[TestCase("costCenter", "costCenter, companyCode, acc_code")]
[TestCase("acc_code", "acc_code, companyCode, costCenter")]
[TestCase("acc_code, companyCode", "acc_code, companyCode, costCenter")]
[TestCase("acc_code, costCenter", "acc_code, costCenter, companyCode")]
public void GetSortExpressionTest(string initial, string exprectedResult)
{
    Assert.AreEqual(exprectedResult, GetSortExpression(initial));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM