-1
\$\begingroup\$

I'm trying to optimize this nested foreach and if statement function for work. My code is retrieving data from a SSMS SQL database with thousands of fields from multiple tables of data, moving them to lists and then looping through the lists to retrieve the data. It is moving all the data to a data grid view to be exported to an excel sheet.

Any ideas on how I can make it run faster?

foreach (Calibrations calibration in Model.PostCalibrationsList)
        {
            if (calibration.Job_No == textBox1.Text)
            {
                foreach (Instruments instrument in Model.InstrumentsList)
                {
                    if (calibration.Inst_ID == instrument.Inst_ID)
                    {
                        foreach (Proc_InstInfo info in Model.Proc_InstInfoList)
                        {
                            foreach (ProcQRef qRef in Model.ProcQRefList)
                            {
                                if (calibration.Inst_ID == instrument.Inst_ID)
                                {
                                    if (instrument.Procedure == info.Proc_No.ToString() && instrument.Procedure == qRef.Procedure.ToString())
                                    {
                                        ex.dataGridView1.Rows.Add(calibration.Order_No, calibration.Inst_ID, instrument.Description, calibration.Cust_Ref, instrument.Manufacturer, instrument.Model_No, instrument.Serial_No, info.Comm1, calibration.Calibration_Required, qRef.StockCode, calibration.Cert_No, info.Proc_No);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
\$\endgroup\$

closed as off-topic by t3chb0t, Graipher, yuri, πάντα ῥεῖ, pacmaninbw May 20 at 19:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Graipher, yuri, πάντα ῥεῖ, pacmaninbw
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    \$\begingroup\$ Could you add a description of what your code is actually doing? All I can gather is you're adding something to a data grid view. And make your title match what it's doing, not your concern about the code. How often is this snippet of code called? Perhaps you should show us the entire function, because as it stands you're just looping through every list and adding it to the data grid view, not really much room for optimization. \$\endgroup\$ – Shelby115 May 20 at 12:46
  • 2
    \$\begingroup\$ 1) Please refer to codereview.stackexchange.com/help/how-to-ask 2) What is the reason you want to optimize this code? Is something running particularly slow? \$\endgroup\$ – Shelby115 May 20 at 12:52
  • \$\begingroup\$ yeah it takes about 20 seconds to open the data grid view form \$\endgroup\$ – Evan Quinn May 20 at 13:17
  • \$\begingroup\$ Your title should describe what your code does, not what you want out of a review. \$\endgroup\$ – BCdotWEB May 20 at 14:12
  • \$\begingroup\$ I think you have a copy/paste bug. There are two if (calibration.Inst_ID == instrument.Inst_ID) nested within each other. Obviously, only the first one matters. \$\endgroup\$ – Jesse C. Slicer May 20 at 14:32
5
\$\begingroup\$

Given that you say you're fetching the data from an SQL database, you should use it. It should be much faster to filter the data with a WHERE in the SQL query than to serialise it, deserialise it, and then filter it without the benefit of any indexes.

Given this tiny fragment of code it's impossible to give more detailed advice on how to do that. It depends enormously on the ORM you're using.

\$\endgroup\$
  • \$\begingroup\$ i tried using where statements within the sql query and it made the process much faster (20 seconds down to 1-2 seconds), thanks! \$\endgroup\$ – Evan Quinn May 21 at 9:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.