Note that there are some explanatory texts on larger screens.

plurals
  1. POThe best way to search in DataTable on multiple conditions in C#?
    primarykey
    data
    text
    <p>I have 2 <code>DataTable</code> with the following columns:</p> <blockquote> <p>Table 1</p> <ul> <li>Title</li> <li>NUMBER</li> <li>Sub_num1 </li> <li>Sub_num2</li> </ul> <p>Table 2</p> <ul> <li>NUMBER</li> <li>Sub_num1</li> <li>Sub_num2</li> </ul> </blockquote> <p>In <code>Table 2</code> Combination of NUMBER, Sub_num1 and Sub_num2 is unique. Can be Many NUMBERS, but with different set of Sub1 and Sub2.</p> <p>In Table 1 Title is unique. A couple titles can have the same NUMBER, but again different set of Subs.</p> <p>I need to loop through <code>Table 2</code> and check if <code>Table 1</code> has exact match with all 3 columns, then get this title, if not I need to get all Titles that have this NUMBER.</p> <p>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...</p> <p>Any help, please?</p> <p><strong>UPDATE</strong> Example:</p> <pre><code> 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 </code></pre> <p>SO I'm looping through Table 2:</p> <pre><code>foreach (DataRow row in dt2.Rows) { // HERE Should be logic and search } </code></pre> <p>RESULT should be: If match print title, if not print ALL titleS with number, that match, so:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload