2013-02-26 27 views
2

我有一组订单,每个订单都有一个期望的重量,一套火鸡,每个都有一个权重,全部包含在数据库和数据集中,但通过对象访问(这使得它更加复杂,但对象在那里以增加项目的复杂性,而不是因为这是一个好主意)。如何将订单与产品进行匹配?

如何匹配它们,然后更新数据库和数据集?我正在尝试使用列表和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类

# <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 

回答

1

的OrderUpdate方法尝试sortedlist,根据键进行排序的值的代码。例如:

Dim OrderList As New SortedList 
    For Count = 0 To Orders.OrderNum - 1 
     OrderList.Add(Orders.OrderDetail(Count, "ApproxWeight"), Count) 
    Next 

将根据Orders.OrderDetail的结果对值进行排序(Count)。这样你就不需要调用SortList方法。

虽然,这

Data.Tables("OrderTbl").Rows(Row).Item("OrderID") = OrderID 

不会改变数据集中的表中的行的订单ID列值,这些变化没有得到承诺的数据库。看看这个msdn walkthrough 关于将数据保存到数据库。基本上,您需要一个具有适当UpdateCommand设置的DataAdapter来更新来自数据集的更改的数据库表。如果您在程序中使用了类型化数据集,那么将更改提交到数据库变得更容易。

相关问题