How to loop over a List while its contents contain a subvalue?
我正试图解析这个单一的方法,它有超过8000行的硬编码数据验证。其中大多数都是相同的,数据源中不同字段的重复逻辑。
很多看起来像这样(C++):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | temp_str = _enrollment->Fields->FieldByName("ID")->AsString.SubString(1,2); if (temp_str.IsEmpty()) { /* do stuff */ } else { if (!IsDigitsOnly(temp_str)) { /* do different stuff */ } else { /* do other stuff */ } } temp_str = _enrollment->Fields->FieldByName("OtherField"); if (temp_str.IsEmpty()) /* do more stuff */ |
所以基本上,我只需要解析出每对
我目前面临的问题是确定每个"块"的开始和结束位置。
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | static void Main(string[] args) { string file = @"C:\somePathToFile.h"; string validationHeader ="temp_str = _enrollment->Fields->FieldByName("; string outputHeader ="====================================================="; int startOfNextValidation; List<string> lines = File.ReadAllLines(file).ToList<string>(); List<string> validations = new List<string>(); while (lines.Contains(validationHeader)) { //lines[0] should be the"beginning" temp_str assignment of the validation //lines[startOfNextValidation] should be the next temp_str assignment startOfNextValidation = lines.IndexOf(validationHeader, lines.IndexOf(validationHeader) + 1); //add the lines within that range to another collection // to be iterated over and written to a textfile later validations.Add((lines[0] + lines[startOfNextValidation]).ToString()); //remove everything up to startOfNextValidation so we can eventually exit lines.RemoveRange(0, startOfNextValidation); } StreamWriter sw = File.CreateText(@"C:\someOtherPathToFile.txt"); foreach (var v in validations.Distinct()) { sw.WriteLine(v); sw.WriteLine(outputHeader); } sw.Close(); } |
我的
所以我想我的第一个问题是,在检查确保这些项目中仍然有包含特定"子值"的项目时,如何循环访问
编辑:
我也试过了;
1 | while (lines.Where(stringToCheck => stringToCheck.Contains(validationHeader))) |
根据此答案;https://stackoverflow.com/a/18767402/1189566
但是它说它不能从一个
你的意思是这样的?
1 | while (lines.Any(x => x.Contains(validationHeader))) |
这将检查
I tried this as well;
1 while (lines.Where(stringToCheck => stringToCheck.Contains(validationHeader)))
这不起作用,因为在您的情况下,Linq的