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.
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 =” not updated the DateTime value? Please help and thank you.
}
The below is the screenshot image of the example code
4
You should use the DateTime.AddMinutes() method
DateEnd = DateStart.addMinutes(59);
You can check more on geeksforgeeks.org and Microsoft