针对相同条件的多个值

时间:2013-12-17 11:24:05

标签: c# optimization

还有其他方法可以优化以下代码吗?我觉得下面的代码对于它执行的操作来说是巨大的。

if ((currentElement == null ||
    (firstGridRow["Low"].ToString() == string.Empty || 
    firstGridRow["High"].ToString() == string.Empty ||
    firstGridRow["Mean"].ToString() == string.Empty ||
    firstGridRow["StdDev"].ToString() == string.Empty)))
{
    continue;
}

if (newRow.Length != 0)
{
    AddColorList(currentElement, opid, currentLow, "Low", newRow, listCollectionLow);
    AddColorList(currentElement, opid, currentHigh, "High", newRow, listCollectionHigh);
    AddColorList(currentElement, opid, currentMean, "Mean", newRow, listCollectionMean);
    AddColorList(currentElement, opid, currentStdDev, "StdDev", newRow, listCollectionStdDev);
} 

1 个答案:

答案 0 :(得分:3)

您可以使用LINQ,如下所示:

private static readonly string[] AllKeys = new[] {"Low", "High", "Mean", "StdDev"};


if (currentElement == null || ALlKeys.Any(k => gridRow[k].ToString() == string.Empty)) {
     ...
}
if (newRow.Length != 0) {
    foreach (var key in AllKeys) {
        AddColorList(currentElement, opid, currentLow, key, newRow, listCollectionLow);
    }
}