我认为在这一点上,我已经看了这么长时间的代码,以至于逻辑变得模糊,我需要更多的关注它。
输入是一系列创建一些 List<> 对象的控件。涉及的变量是 importData,一个包含“sarray”的列表,它是包含字段值列表的字符串 []。为 "validIndeces" 和 "indecesCantBeDuplicated" 引用的 Lists<> 包含我们实际需要的 importData 的 sarray 中项目的索引,其中哪些不能在同一字段中有重复项(即,没有两个公司 ID 相同)。
这里的目标是取importData的string[]s,过滤掉查询中我们不需要的,执行一些指令使它们与某个表中的数据兼容,然后检查确保一个表中不存在重复给定的字段。如果在“indecesCantBeDuplicated”中指示了一个字段,并且之前在同一索引中的 importData 中找到了匹配项,那么它应该移动到下一个 string[] 对象,否则将该行添加到查询中。
输出基本上是 SQL 中的一系列插入语句,但该代码很好。这就是我卡住的地方。为什么我不过滤掉重复项?
//go through each string array in the import (i.e. each record to import)
foreach (string[] sarray in importData) {
List<List<string>> QueryList = new List<List<string>>();
List<string> Queriables = new List<string>();
for (int i = 0; i < sarray.Count(); i++) {
bool addit = true;
if (validIndeces.Contains(i)) {
//ensure this is one of the indeces used to import
if (indecesCantBeDuplicated.Contains(i)) {
// if THIS index can't have duplicate values
foreach (string[] s2 in importData) {
//check each record in the import
foreach(string s in s2) {
//check each field in the record
if (s2.ElementAt(i) == sarray[i]) {
//if the field value at this index matches the one we're about to put in
addit = false;
} else {
addit = true;
}
}
}
}
if (indecesToDelimit.Contains(i)) {
//ensure that delimiters are added to all text field values
sarray[i] = "'" + sarray[i] + "'";
}
if (addit) {
Queriables.Add(sarray[i]);
}
}
}
}