VBStarterKid
Member
Hello there,
I have a vb.net algorithm that should calculate the daily and monthly sum of productive hours of the employees from the sales table in an SQL Server database.
What my program already does is that it has extracted the relevant employees' records from the sales table based on the selected month and year from the user interface screen, with all the records going into a DataSet. At this point, my dataset already contains the records necessary for calculation. Since I want to calculate the daily productive hour of each employee, I need to select all the records for that particular employee from the dataset on that particular day.
I have devised a For Loop that loops each day, starting form 1st of the month to the 31st, depending on the month selected.
Now, my problem...the earlier algorithm OUTER LOOPS each datarow in the dataset, and INNER LOOPS each day in the month. basically it means for each record in my dataset, the program loops roughly 31 times just to check whether the particular employee was productive on the selected day. If the dataset contains 100 records, the looping will run 100 * 31 times, and what if only 1 record satisfies the condition that I set. That is wasting time and resources.
I found that I can use DataTable.Select method instead to filter out the datarows based on the condition that is sent to the method as a parameter, but I can't use wildcards in the middle of the string. I wanted to make the day value in my date-based condition as a dynamic value, changing as necessary following the FOR LOOP that I mentioned earlier. For example, in my FOR loop, the condition is that the records I want to select for calculation must have a sales date of #mm/1/yyyy# with the value 1 there dynamicaly changing as the loop progresses. The next loop, I would want the records with a sales date of #mm/2/yyyy#. And so on for the next loops.
How can I do that?
Thanks for the invaluable advice.
The following is my original code snippet THAT works, but not efficient:
My plan now is to modify the algorithm to use the following method:
Is my logic correct?
I have a vb.net algorithm that should calculate the daily and monthly sum of productive hours of the employees from the sales table in an SQL Server database.
What my program already does is that it has extracted the relevant employees' records from the sales table based on the selected month and year from the user interface screen, with all the records going into a DataSet. At this point, my dataset already contains the records necessary for calculation. Since I want to calculate the daily productive hour of each employee, I need to select all the records for that particular employee from the dataset on that particular day.
I have devised a For Loop that loops each day, starting form 1st of the month to the 31st, depending on the month selected.
Now, my problem...the earlier algorithm OUTER LOOPS each datarow in the dataset, and INNER LOOPS each day in the month. basically it means for each record in my dataset, the program loops roughly 31 times just to check whether the particular employee was productive on the selected day. If the dataset contains 100 records, the looping will run 100 * 31 times, and what if only 1 record satisfies the condition that I set. That is wasting time and resources.
I found that I can use DataTable.Select method instead to filter out the datarows based on the condition that is sent to the method as a parameter, but I can't use wildcards in the middle of the string. I wanted to make the day value in my date-based condition as a dynamic value, changing as necessary following the FOR LOOP that I mentioned earlier. For example, in my FOR loop, the condition is that the records I want to select for calculation must have a sales date of #mm/1/yyyy# with the value 1 there dynamicaly changing as the loop progresses. The next loop, I would want the records with a sales date of #mm/2/yyyy#. And so on for the next loops.
How can I do that?
Thanks for the invaluable advice.
The following is my original code snippet THAT works, but not efficient:
VB.NET:
' Loop each record in the dataset, additionally, for each record loop,
' inner loop the number of days in the selected month
For Each dr In ds.Tables(0).Rows
For i = 1 To nDayInMonth
' Advance the date of loop by i count
dtLoopDate = dtLoopDate.AddDays(-dtLoopDate.Day + i)
' RecordDate is the sales date time that the current datarow contains
dtRecordDate = CType(dr("StartTime"), Date)
' If the record date is within 24 hours of the date in the current loop then do the statements
If dtRecordDate >= dtLoopDate And dtRecordDate < dtLoopDate.AddDays(1) Then
.....
...
End If
Next
Next
My plan now is to modify the algorithm to use the following method:
VB.NET:
Dim rows() As DataRow
Dim expression As String
Dim dtLoopDate As Date
For i = 1 To nDayInMonth
' Advance the date of the month by i count
dtLoopDate = dtLoopDate.AddDays(-dtLoopDate.Day + i)
expression = "dtLoopDate = CType(dr("StartTime"), Date)"
rows = ds.Tables(0).Select(expression)
Next
Is my logic correct?
Last edited: