开发者

More efficient for each loops

Is there a more efficient way of doing this code?

For Each row As DataRow In dt.Rows
    Dim ts1 As String = row(0).ToString
    For index As Integer = 1 To 9
        Dim colName As String
        colName = dt.Columns(index).ToString
        For Each row2 As DataRow In dtAppAvail.Rows
            Dim colName2 As String
            Dim ts2 As String
            colName2 = row2("Day").ToString.Substring(0, 3) & " " & CType(row2("Date"), Date).ToString("dd/MM")
            ts2 = row2("Timeslot").ToString
            If colName = colName2 AndAlso ts1 = ts2 Then
                row(index) = row2("AppointmentsBooked")
            End If
        Next开发者_如何学编程
    Next                            
Next


For Each row As DataRow In dt.Rows
            For index As Integer = 1 To 9
                For Each row2 As DataRow In dtAppAvail.Rows
                    Dim colName2 As String = row2("Day").ToString.Substring(0, 3) & " " & CType(row2("Date"), Date).ToString("dd/MM")
                    If dt.Columns(index).ToString = colName2 AndAlso row(0).ToString = row2("Timeslot").ToString Then
                        row(index) = row2("AppointmentsBooked")
                    End If
                Next
            Next
        Next

Here is a more condensed version... it uses less variables which i assume = less memory storage, but i doubt you will notice much of a difference...


If your Datatable is huge, then make the loop parallel. I would have recommended on using parallel.foreach which comes with .NET 4, but since you specified .NET 2, you can implement it by yourself.

Check the "Patterns for Parallel Programming":

http://www.microsoft.com/download/en/details.aspx?id=19222
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜