一旦 If 语句为真,循环就会停止,我不知道如何继续循环

As soon as If statement is true the loop stops, I dont know how to continue the loop

一旦程序代码检查了 excel 文件中无效的第一列,它就不会继续检查其余部分。有没有办法继续循环并检查其余列?

ValidationFile 代码如下。它遍历 Excel 列名并检查列大小是否不超过 128 个字符并且不为空或整个列不为空。

 public Boolean InvalidColumnNames()
        {
            using (ReadAndStreamFile())
            {
                reader.Read();
                {
                    int counter = 0;
                    var ColumnsNames = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToList();
                    if (ColumnsNames.Count != 0 && reader.Read() == true)
                    {
                        for (int columnNumber = 0; columnNumber < ColumnsNames.Count; columnNumber++)
                        {
                            var excel = new ExcelDataReaderFile();
                            var cellAddress = excel.GetAddress(counter, 0);
                            counter += 1;
                            if (ColumnsNames[columnNumber] != null && ColumnsNames[columnNumber].ToString().Length > columnNameSizeLimit)
                            {
                               Console.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is {columnNumber.ToString().Length} characters long and exceeds {columnNameSizeLimit} character column name limit. Supply a valid column name.");
                                return true;
                            }
                            else if (ColumnsNames[columnNumber] == null)
                            {
                                Conseol.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is empty. Supply a valid column name.");
                                return true;
                            }
                            else
                            {
                               // return true;
                            }
                            continue;
                        }
                    }
                    else
                    {
                        Console.WriteLine($"[{GetFileName(file)}]{reader.Name} is empty and cannot be validated. Supply a non-empty file.");
                        return true;
                    };
                }
                reader.Dispose();
                reader.Close();
                return false;
            }
        }

下面是 Program Main 方法,它描述如果 Invalid column names 为 true,则给出消息验证这样的文件失败,否则它已通过验证。

 class program
        {
            public static void Main(string[] args)
            {
                string path = ConfigurationManager.AppSettings["path"].ToString();
                string search = ConfigurationManager.AppSettings["search"].ToString();


                ExcelDataReaderFile ExcelDataReaderFile = new ExcelDataReaderFile();
                string[] fileEntries = Directory.GetFiles(path, "*" + search + "*", SearchOption.AllDirectories);

                dynamic counter = 0;
                BasicValidation BasicValidation = new BasicValidation();
                    foreach (string file in fileEntries)
                    {
                        BasicValidation basicValidation = new BasicValidation(file, 128, 32767, 128, 128);
                        counter += 1;

                        Logger.Info($"Validating {basicValidation.GetFileName(file)}");

                        //Console.ForegroundColor = ConsoleColor.Blue;
                        if (basicValidation.InvalidColumnNames())
                      {
                            //if the above statments are true then run those methods and log an error
                            //Console.WriteLine($"{counter}) {basicValidation.GetFileName(file)} in {Regex.Unescape(path)} has failed validation. Check the error log for details.");
                            Console.WriteLine("Press any key to continue...");
                            Console.ReadKey();
                        }
                        else
                        {
                            Console.WriteLine($"{counter}) {basicValidation.GetFileName(file)} in {file} has passed validation checks.");
                            Console.WriteLine("Press any key to continue...");
                            Console.ReadKey();
                        }
                }
                else
                {
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
     }

如果您在任何 for/foreach 循环中编写 return 语句,循环将终止执行并从那里 returns 开始。 因此,使用变量并根据条件为其赋值,一旦循环从方法完成 return

        public Boolean InvalidColumnNames()
        {
            bool errorInFile = false;
            using (ReadAndStreamFile())
            {
                reader.Read();
                {
                    int counter = 0;
                    var ColumnsNames = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToList();
                    if (ColumnsNames.Count != 0 && reader.Read() == true)
                    {
                        for (int columnNumber = 0; columnNumber < ColumnsNames.Count; columnNumber++)
                        {
                            var excel = new ExcelDataReaderFile();
                            var cellAddress = excel.GetAddress(counter, 0);
                            counter += 1;
                            if (ColumnsNames[columnNumber] != null && ColumnsNames[columnNumber].ToString().Length > columnNameSizeLimit)
                            {
                                Console.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is {columnNumber.ToString().Length} characters long and exceeds {columnNameSizeLimit} character column name limit. Supply a valid column name.");
                                errorInFile =  true;
                            }
                            else if (ColumnsNames[columnNumber] == null)
                            {
                                Conseol.WriteLine($"[{GetFileName(file)}]{reader.Name}!{cellAddress} is empty. Supply a valid column name.");
                                errorInFile =  true;
                            }
                            else
                            {
                                // return true;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine($"[{GetFileName(file)}]{reader.Name} is empty and cannot be validated. Supply a non-empty file.");
                        errorInFile =  true;
                    };
                }
                reader.Dispose();
                reader.Close();

                return errorInFile;
            }
        }