C# 如果statemets

C# 如果statemets,c#,asp.net,if-statement,C#,Asp.net,If Statement,因此,我正在导入一个excel文档到我的项目中。然后,如果电子表格中的任何列为空,我将使用if语句显示相应的错误消息。我的代码正在运行,但看起来不整洁。是否需要将所有if语句重构成一个循环来显示错误消息,可能是另一种检查所有if语句的方法??CheckIfColumnSempty是一个bool方法,如果列为空,则返回true //if either column 0 and 1 are empty && column 2,3,4 and 5 are not if (!ch

因此,我正在导入一个excel文档到我的项目中。然后,如果电子表格中的任何列为空,我将使用if语句显示相应的错误消息。我的代码正在运行,但看起来不整洁。是否需要将所有if语句重构成一个循环来显示错误消息,可能是另一种检查所有if语句的方法??CheckIfColumnSempty是一个bool方法,如果列为空,则返回true

//if either column 0 and 1 are empty && column 2,3,4 and 5 are not
    if (!checkIfColumnisEmpty(r.ItemArray[0]) || !checkIfColumnisEmpty(r.ItemArray[1])
        && checkIfColumnisEmpty(r.ItemArray[2]) && checkIfColumnisEmpty(r.ItemArray[3])
         && checkIfColumnisEmpty(r.ItemArray[4]) && checkIfColumnisEmpty(r.ItemArray[5]))
    {
        if (checkIfColumnisEmpty(r.ItemArray[0]) && !checkIfColumnisEmpty(r.ItemArray[1]))
        {
            throw new ImportBOQException("Error importing document: First column is empty");
        }
        else if (!checkIfColumnisEmpty(r.ItemArray[0]) && checkIfColumnisEmpty(r.ItemArray[1]))
        {
            throw new ImportBOQException("Error importing document: Second column is empty");
        }

        else if (!checkIfColumnisEmpty(r.ItemArray[0]) && !checkIfColumnisEmpty(r.ItemArray[1]))
        {
            //all columns are valid so...
            Column0inSpreadsheet = r.ItemArray[0] as string;
            Column1inSpreadsheet = r.ItemArray[1] as string;

          //Other code which performs other operations, once the level as reached this far
        }
    }                                

    //if column 0 and 1 are NOT empty && Either column 2,3,4 or 5 is empty
    else if (checkIfColumnisEmpty(r.ItemArray[0]) && checkIfColumnisEmpty(r.ItemArray[1])
          || !checkIfColumnisEmpty(r.ItemArray[2]) || !checkIfColumnisEmpty(r.ItemArray[3])
           || !checkIfColumnisEmpty(r.ItemArray[4]) || !checkIfColumnisEmpty(r.ItemArray[5]))
    {
        if (checkIfColumnisEmpty(r.ItemArray[2]))
        {
            throw new ImportBOQException("Error importing document: Third column is empty");
        }
        else if (checkIfColumnisEmpty(r.ItemArray[3]))
        {
            throw new ImportBOQException("Error importing document: Fourth column is empty");
        }
        else if (checkIfColumnisEmpty(r.ItemArray[4]))
        {
            throw new ImportBOQException("Error importing document: Fifth column is empty");
        }
        else if (checkIfColumnisEmpty(r.ItemArray[5]))
        {
            throw new ImportBOQException("Error importing document: Sixth column is empty");
        }
        else
        //all columns are valid so...
        {   Column2inSpreadsheet = (r.ItemArray[2]) as string;
            Column3inSpreadsheet = (r.ItemArray[3]) as string;
            Column4inSpreadsheet = (r.ItemArray[4]) as string;
            Column5inSpreadsheet = (r.ItemArray[5]) as string;

            //Other code which performs other operations, once the level as reached this far
        }
    }
    else
    //other errors ot related to empty colums
    {
        throw new Exception("Error Uploading");
    }
}

首先,将r转换为具有命名属性的类,而不是对数组使用索引器,并从源中初始化集合。 第二,创建带有签名的helper方法
布尔验证列(字符串值、字符串属性名称)。这是一个基本的重构,也许您可以将逻辑从“Holy s***”更改为“that's mass but we can change!”

如果您只需要检查其中一列是否为“empty”并获取其索引,您可以使用以下方法:

var firstEmptyColumn = r.ItemArray
    .Select((f, i) => new { Field=f, Index=i })
    .FirstOrDefault(x => r.IsNull(x.Index) || x.Field.ToString().Trim().Length == 0);
if (firstEmptyColumn != null)
{
    string errorMsg = string.Format("Error importing document: {0} column is empty",
        firstEmptyColumn.Index);
    throw new ImportBOQException(errorMsg);
}

我认为这个问题属于ok,在那里发布了谢谢,谢谢,ItemArray是从电子表格中导入的列,例如r.ItemArray[0]是第一列ok谢谢Tim,我会试一试