还有其他方法可以优化以下代码吗?我觉得下面的代码对于它执行的操作来说是巨大的。
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);
}
答案 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);
}
}