I think at this point I’ve just looked at this code so long that the logic is getting fuzzy and I need more eyes on it.
Input is a series of controls that create a few List<> objects. Variables involved are importData, a List which contains “sarray”s, which are string[]s containing a list of field values. The Lists<> referenced for “validIndeces” and “indecesCantBeDuplicated” contain the indeces of items in importData’s sarrays that we actually need, and which ones of those can’t have a duplicate in the same field (i.e., no two company ID’s the same).
The goal here is to take importData’s string[]s, filter out the ones we don’t need in the query, execute some instruction to make them compatible with data in a certain table, and then check to ensure that no duplicates exist in a given field. If a field is indicated in “indecesCantBeDuplicated” and a match is found earlier in the importData in the same index, then it should move on to the next string[] object, otherwise add the line to the query.
Output basically comes out to a series of Insert statements in SQL, but that code is fine. Here’s where I’m stuck. Why am I not filtering out the duplicates on this?
//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]);
}
}
}
}
You have to break out of your foreach when you detect an
addit == false. Otherwise, it will just be set totrueagain on the next loop iteration.