C# How to update variable value in a loop

c ahmad shukri Yong 26 Reputation points
2023-01-09T10:08:26.23+00:00

Hi expert, I've try to loop and from the below code, The problem is when it loop back to “IEnumerable selectedRows =”. The value of DateStart and DateEnd remain the same “9/1/2003 2:00:00” and “9/1/2003 2:59:00” respectively as its initial value. Why the “IEnumerable selectedRows =” do not update the DateTime value? Please help and thank you.

Below is the code;

DateTime DateStart = Convert.ToDateTime("9/1/2023 2:00:00");  
DateTime DateEnd = DateStart + TimeSpan.FromMinutes(59);  
  
for (int intCount=0; intCount < 8; intCount++)  
{  
                      
    IEnumerable<DataRow> selectedRows = dtData.AsEnumerable().Where(row => (row.Field<DateTime>("Date") >= DateStart) && row.Field<DateTime>("Date") <= DateEnd);  
  
  
      foreach (DataRow row in selectedRows)  
      {  
    
         //Do some stuffs  
  
      }  
        
      //store the result in DataTable  
      DataRow dtDataRowResult = dtDataResult.NewRow();  
      dtDataRowResult[0] = DateStart;  
      dtDataRowResult[1] = result1;  
      dtDataRowResult[2] = result2;  
      dtDataResult.Rows.Add(dtDataRowResult);  
  
      //when code go this line below, it should be added one hour from the previous hour, so now it is “9/1/2003 3:00:00”  
      DateStart = DateStart.AddHours(1);  
     //and the DateEnd should be added to “9/1/2003 3:59:00”  
      DateEnd = DateStart + TimeSpan.FromMinutes(59);  
     //The problem is when it loop back to “IEnumerable<DataRow> selectedRows =”. The value of DateStart and DateEnd remain the same “9/1/2003 2:00:00” and “9/1/2003 2:59:00” respectively as the initial value. Why the “IEnumerable<DataRow> selectedRows =” do not updated the DateTime value? Please help and thank you.  
  

The below is the screenshot image of the example code;

277397-screenshot-2023-01-09-180524-msdn-forum.png

Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. c ahmad shukri Yong 26 Reputation points
    2023-01-09T11:59:27.003+00:00

    I've done in console app, everything is works properly and I apply this code to my previous code, it works properly. Anyway thanks.

    Below is the example of the code.

    static void Main(string[] args)  
            {  
                DataTable dtDataResult = new DataTable();  
                DataColumn dcDataResult = dtDataResult.Columns.Add("DateTime", typeof(DateTime));  
                dcDataResult = dtDataResult.Columns.Add("Count", typeof(Int16));  
      
                DateTime DateStart = Convert.ToDateTime("9/1/2023 2:00:00");  
                DateTime DateEnd = DateStart + TimeSpan.FromMinutes(59);  
      
                for (int intCount = 0; intCount < 8; intCount++)  
                {  
                    int intTest = 0;  
                    TimeSpan tsGetHours = DateEnd - DateStart;  
      
                    for (int intDataRow=0; intDataRow< 10; intDataRow++)  
                    {  
                        intTest++;  
      
                    }  
                    DataRow dtDataRowResult = dtDataResult.NewRow();  
                    dtDataRowResult[0] = DateStart;  
                    dtDataRowResult[1] = intTest;  
                    dtDataResult.Rows.Add(dtDataRowResult);  
      
                    DateStart = DateStart.AddHours(1);  
                    DateEnd = DateStart + TimeSpan.FromMinutes(59);  
      
                }  
      
            }  
    

    below is the screenshot image of the code.

    277443-screenshot-2023-01-09-195244-answer-code.png

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.