I have 2 DataTable
with the following columns:
Table 1
- Title
- NUMBER
- Sub_num1
- Sub_num2
Table 2
- NUMBER
- Sub_num1
- Sub_num2
In Table 2
Combination of NUMBER, Sub_num1 and Sub_num2 is unique. Can be Many NUMBERS, but with different set of Sub1 and Sub2.
In Table 1 Title is unique. A couple titles can have the same NUMBER, but again different set of Subs.
I need to loop through Table 2
and check if Table 1
has exact match with all 3 columns, then get this title, if not I need to get all Titles that have this NUMBER.
What is the best and fastest way to do this search? On the top of my head I have only next: Loop through records in Table 2 and for each record loop through Table 1 and check for match, but I think that this process can be very resource-intensive...
Any help, please?
UPDATE Example:
var dt1 = new DataTable("Table 1");
dt1.Columns.Add("title", typeof(string));
dt1.Columns.Add("number", typeof(int));
dt1.Columns.Add("subnum1", typeof(int));
dt1.Columns.Add("subnum2", typeof(int));
dt1.Rows.Add(new object[] { "a", 1111, 1, 1 }); // Exact match!
dt1.Rows.Add(new object[] { "b", 2222, 1, 1 }); // Only NUMBER match
dt1.Rows.Add(new object[] { "b", 2222, 2, 2 }); // Only NUMBER match
dt1.Rows.Add(new object[] { "d", 3333, 1, 1 }); // Exact match!
dt1.Rows.Add(new object[] { "d", 3333, 1, 2 });
dt1.Rows.Add(new object[] { "d", 3333, 2, 1 });
var dt2 = new DataTable("Table 2");
dt2.Columns.Add("number", typeof(int));
dt2.Columns.Add("subnum1", typeof(int));
dt2.Columns.Add("subnum2", typeof(int));
dt2.Rows.Add(new object[] { 1111, 1, 1 }); // Exact match!
dt2.Rows.Add(new object[] { 2222, "", 5 }); // Only NUMBER match
dt2.Rows.Add(new object[] { 3333, 1, 1 }); // Exact match!
dt2.Rows.Add(new object[] { 3333, "", "" }); // Only NUMBER match
SO I'm looping through Table 2:
foreach (DataRow row in dt2.Rows)
{
// HERE Should be logic and search
}
RESULT should be: If match print title, if not print ALL titleS with number, that match, so:
1. "a", 1111, 1, 1
2.1 "b", 2222, 1, 1
2.2 "b", 2222, 2, 2
3. "d", 3333, 1, 1
4.1 "d", 3333, 1, 1
4.2 "d", 3333, 1, 2
4.3 "d", 3333, 2, 1