Forums - C# Corner

Forum guidelines
Abraham Olatubosun

Abraham Olatubosun

  • 1.3k
  • 152
  • 8k

How to stop FOREACH loop without breaking out of the loop

Jul 17 2017 12:20 PM
Dear friends,
Best of the day, i will apreciate any solution from you,
i am writting a program that loop through set of records in a database
this database contain duplicate records to loop through the records i am
fist use a sql statement to pass the records to a datatable
  1. Select Distinct * from tbl_PatientsTable  
 
i then use the following statement to loop through the record, where i find thesame record ID coming back it should skip it and take the next record it that way i will not display duplicates,
  1. var PatientIDs = "";  
  2.            DT1 = ConnectAll.LastDrugPickup(TextBox1.Text.Trim(), TextBox2.Text.Trim());  
  3.   
  4.            foreach (DataRow r in DT1.Rows)  
  5.            {  
  6.                if (PatientIDs == r["PatientID"].ToString().Trim()) { break; }  
  7.             do other things ......  
  8.           }  
 
But my problem is break command make the loop to happend only once.
please how can i resolve this without using break and be able to fetch my records with no duplicates, since DISTINCT could not help.

Answers (1)