I have a dataset with three tables in it. I want to compare a column item from one table against column items from the other two but to do this I am using nested foreach loops. The code is returning way more data than it should be so I have a feeling I messed something up; also I’m sure there is a better/cleaner way to do this, feel free to let me know.
foreach (DataRow row in dataSet.Tables["Taxonomy"].Rows)
{
var projectTypes = row["ProjectType"].ToString();
var tier3 = row["Tier3Project"].ToString();
if (tier3.Equals(""))
tier3 = "null";
Console.WriteLine(projectTypes);
foreach (DataRow dRow in dataSet.Tables["DefaultEventTypes"].Rows)
{
var name = dRow["name"].ToString();
if (name.Equals(""))
name = "null";
/****** Begin Comparisons ******/
if (projectTypes.Trim().ToLower().Equals(name.Trim().ToLower()))
{
foreach (DataRow sRow in dataSet.Tables["ScheduleEvents"].Rows)
{
var nameShort = sRow["nameShort"].ToString();
/****** Compare to ScheduleEvent ******/
if (projectTypes.Trim().ToLower().Equals(nameShort.Trim().ToLower()))
{
//Update both DefaultEventType and ScheduleEvent
Console.WriteLine(projectTypes + " " + name + " " + tier3);
counter++;
}
else
{
}
}
}
else
{
foreach (DataRow sRow in dataSet.Tables["ScheduleEvents"].Rows)
{
var nameShort = sRow["nameShort"].ToString();
/****** Compare to ScheduleEvent ******/
if (projectTypes.Trim().ToLower().Equals(nameShort.Trim().ToLower()))
{
//Update ScheduleEvent
}
}
}
/****** End Comparisons ******/
}
}
I can clarify if needed, thanks!
Edit:
I want to pull an item from table ‘Taxonomy’ and then compare it to every row in table ‘DefaultEventTypes’. If there is a match, I then want to compare the ‘Taxonomy’ item to every row in ‘ScheduleEvent’.
Eventually I want to add in update statements to change matching items to a new name and save the tables back to the database.
It seems to be doing comparisons but the number of values it’s returning is far too large.
Something like that…didn’t test this, though
PS: this should return you a set of values, that are present in all 3 tables in respective columns.
Answer to comment:
Well, you have enough information to update things you want.
This example will update all the values in the second table so that they are no longer similar to those in other 2 tables. PS: again, didn’t test that. syntax error are possible