Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Oh boy, code with lots of opportunities for simplification. You can reduce the scope of local variables, removing any temptation to assign them unused values. You can also convert For loops to ForEach loops when you don't use the index except to access a collection.</p> <p>Initial simplification:</p> <pre><code>private void RunMatchingAlgorithm() { foreach (var databaseRow in dtFromTableContracts.Rows) { string partNumber2 = databaseRow["supplier_part_no"].ToString(); string contract_no2 = databaseRow["contract_no"].ToString(); DateTime expiration_date2 = Convert.ToDateTime(databaseRow["con_end_date"]).Date; foreach (var excelRow in dtConversion.Rows) { string contract_no = excelRow["vend_contract_no"].ToString(); //If we have even a partial match, check for a part number match if (contract_no2.StartsWith(contract_no)) { string partNumber = excelRow["vend_item_id"].ToString(); //If the values match, populate from both tables if (partNumber == partNumber2) { excelRow["wpd_expiration_date"] = expiration_date2.Date; excelRow["wpd_cont_cost"] = databaseRow["contract_cost"]; excelRow["wpd_contract_no"] = databaseRow["contract_no"]; excelRow["wpd_item_id"] = databaseRow["supplier_part_no"]; excelRow["wpd_item_no"] = databaseRow["item_id"]; excelRow["discontinued"] = databaseRow["discontinued"]; excelRow["job_no"] = databaseRow["job_no"]; } } } } } </code></pre> <p>Come to think of it, this is pretty much the exact case linq queries were designed for. We can transform most of the code into a query:</p> <pre><code>private void RunMatchingAlgorithm() { var matches = from databaseRow in dtFromTableContracts.Rows let partNumber2 = databaseRow["supplier_part_no"].ToString() let contract_no2 = databaseRow["contract_no"].ToString() let expiration_date2 = Convert.ToDateTime(databaseRow["con_end_date"]).Date from excelRow in dtConversion.Rows let contract_no = excelRow["vend_contract_no"].ToString() where contract_no2.StartsWith(contract_no) let partNumber = excelRow["vend_item_id"].ToString() where partNumber == partNumber2 select new { databaseRow, excelRow, expiration_date2 } foreach (var m in matches) { var dst = m.excelRow; var src = m.databaseRow; dst["wpd_expiration_date"] = m.expiration_date2.Date; dst["wpd_cont_cost"] = src["contract_cost"]; dst["wpd_contract_no"] = src["contract_no"]; dst["wpd_item_id"] = src["supplier_part_no"]; dst["wpd_item_no"] = src["item_id"]; dst["discontinued"] = src["discontinued"]; dst["job_no"] = src["job_no"]; } } </code></pre> <p>and now I see where an optimization can be applied. We're doing a nested 'from' with a 'where', and that's equivalent to a cross-join. Also, we can cut most of the now-only-used-once temporaries:</p> <pre><code>private void RunMatchingAlgorithm() { var matches = from databaseRow in dtFromTableContracts.Rows join excelRow in dtConversion.Rows on excelRow["vend_item_id"].ToString() equals databaseRow["supplier_part_no"].ToString() where databaseRow["contract_no"].ToString().StartsWith(excelRow["vend_contract_no"].ToString()) select new { databaseRow, excelRow } foreach (var m in matches) { var dst = m.excelRow; var src = m.databaseRow; dst["wpd_expiration_date"] = Convert.ToDateTime(src["con_end_date"]).Date; dst["wpd_cont_cost"] = src["contract_cost"]; dst["wpd_contract_no"] = src["contract_no"]; dst["wpd_item_id"] = src["supplier_part_no"]; dst["wpd_item_no"] = src["item_id"]; dst["discontinued"] = src["discontinued"]; dst["job_no"] = src["job_no"]; } } </code></pre> <p>I actually haven't used cross-joins much, but I assume they use a hash table under the hood to have O(n+m) complexity instead of O(n*m). If both tables were in the database, then the database could take advantage of already-constructed hash tables / indexes.</p> <p>You might also want to consider some sort of generated Linq2SQL class, so you can have type-safe access to row fields.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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