我有一组订单,每个都有所需的重量,还有一组火鸡,每个都有一个重量,都包含在数据库和数据集中,但通过对象访问(这使得它更复杂,但对象在那里增加项目的复杂性,而不是因为这是一个好主意)。
如何匹配它们,然后更新数据库和数据集?我正在尝试使用列表和 LINQ 来实现,但效果不佳。不幸的是,我看不出哪里出错了。编辑:该行
For Count = 0 To Max - 1
Orders.UpdateOrder(Count, Count + 1, Orders.OrderDetail(Count, "CustomerID"), TurkeyList(Count)(0), Orders.OrderDetail(Count, "ApproxWeight"))
Next
的索引超出范围错误。我知道这意味着什么,但我不明白我为什么会得到它,以及如何解决它。
我已经完成了我的研究——事实上,我目前的尝试使用了列表(在我读到我可以在 StackOverflow 上使用它们之前我从未听说过)但是,tbh,这不是我被教过的东西,而且我可以使用一些指导。我最初尝试使用二维数组,但没有成功。我认为我当前的实现应该可以工作 - 所以我很有可能只是错过了一些愚蠢的东西。以下是我的代码的相关部分。
Imports System.Linq
# <summary>
# SortForm is a public class which handles the events and controls of a single form.
# </summary>
# <remarks></remarks>
Public Class SortForm
Dim Turkeys As New TurkeyDBInteract
Dim Orders As New OrderDBInteract
Dim Customers As New CustomerDBInteract
Public TurkeyList As New List(Of Integer())
Public OrderList As New List(Of Integer())
# <summary>
# The FillLists subroutine fills the two lists with data from the dataset.
# </summary>
# <remarks></remarks>
Public Sub FillLists()
For Count = 0 To Orders.OrderNum - 1
OrderList.Add(New Integer() {Count, Orders.OrderDetail(Count, "ApproxWeight")})
Next
For Count = 0 To Turkeys.TurkeyNum - 1
TurkeyList.Add(New Integer() {Count, Turkeys.TurkeyDetail(Count, "Weight")})
Next
End Sub
# <summary>
# The SortList subroutine takes a List of 1-dimensional integer and sorts it using LINQ to Objects.
# </summary>
# <param name="List"></param>
# <remarks></remarks>
Public Sub SortList(ByRef List As List(Of Integer()))
Dim SortedList As New List(Of Integer())
SortedList = List.OrderBy(Function(Weight) Weight(1)).ToList()
List = SortedList
End Sub
# <summary>
# This is the SortButton click event. This calls the <see cref="FillLists"/> and <see cref="SortList"/> subroutines to process order and turkey data, then matches the lists and uses this to update the dataset and database.
# </summary>
# <param name="sender"></param>
# <param name="e"></param>
# <remarks></remarks>
Private Sub SortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortButton.Click
FillLists()
SortList(TurkeyList)
SortList(OrderList)
Dim Max As Integer
If TurkeyList.Count > OrderList.Count Then
Max = OrderList.Count
Else
Max = TurkeyList.Count
End If
For Count = 0 To Max - 1
Orders.UpdateOrder(Count, Count + 1, Orders.OrderDetail(Count, "CustomerID"), TurkeyList(Count)(0), Orders.OrderDetail(Count, "ApproxWeight"))
Next
End Sub
End Class
这是 OrderDBInteract 类中 OrderUpdate 方法的代码
# <summary>
# Subroutine UpdateOrder updates every value of a specific record of the OrderTbl dataset table and then updates the database.
# </summary>
# <param name="Row">Row passes the row of the record to be updated.</param>
# <param name="OrderID">OrderID passes the new primary key value of the record to be updated.</param>
# <param name="CustomerID">CustomerID passes the new foreign key field value of the record to be updated.</param>
# <param name="ApproxWeight">ApproxWeight passes a new field value of the record to be updated.</param>
# <remarks></remarks>
Public Sub UpdateOrder(ByVal Row, ByVal OrderID, ByVal CustomerID, ByVal TurkeyID, ByVal ApproxWeight)
Data.Tables("OrderTbl").Rows(Row).Item("OrderID") = OrderID
Data.Tables("OrderTbl").Rows(Row).Item("CustomerID") = CustomerID
Data.Tables("OrderTbl").Rows(Row).Item("TurkeyID") = TurkeyID
Data.Tables("OrderTbl").Rows(Row).Item("ApproxWeight") = ApproxWeight
End Sub