Manipulate和Dedupe数据数组,用于显示到DataGrid中

时间:2013-12-05 15:29:30

标签: arrays vb.net datagrid streamreader

我的VB .Net程序中有一系列数据,遵循以下格式。

Order No.|Description|Colour|Size|Total Qty.|Order Date|Expected D.D.
2273448|NOK OPAQUE KNEE HIGH |BLACK |X |001 |02/12/2013 |05/12/2013
2231428|UX XR WARM HOLD UPS |NAVY |ONE SIZE |001 |02/12/2013 |05/12/2013
2231428|NHS SOFTHOLD HOLD-UP |BLACK |X |001 |02/12/2013 |05/12/2013
2265640|KX XR WARM TIGHTS |BLACK |XXL |001 |02/12/2013 |05/12/2013
2273448|NOK OPAQUE KNEE HIGH |BLACK |X |001 |02/12/2013 |05/12/2013
2231428|NHS SOFTHOLD HOLD-UP |BLACK |X |001 |02/12/2013 |05/12/2013
2231428|NHS SOFTHOLD HOLD-UP |NATURAL |X |001 |02/12/2013 |05/12/2013
2267461|WF-01 FLIP FLOP |BLACK |13 |001 |02/12/2013 |05/12/2013
2231428|NHS SOFTHOLD HOLD-UP |NATURAL |X |001 |02/12/2013 |05/12/2013
2273007|CR COTTON-RICH SOCK |BLACK |4-7 |001 |02/12/2013 |06/12/2013
2273127|TH THERMAL SOCK |MOSS |6-11 |001 |02/12/2013 |06/12/2013

这是一个我无法控制的预格式化的管道文本文件。

我正在尝试将这些数据带入VB DataGrid,但在此之前,我想重复数值,同时计算它们,因此我只能显示相关数据,没有重复行,以便程序用户然后可以选择行并处理所有相关的行(即减少人为错误的可能性。

基于上述情况,DataGrid的预期输出将类似于......

Order No | Lines on Order | Order Date | Expected Date
2273448  |2               |02/12/2013  |05/12/2013
2231428  |5               |02/12/2013  |05/12/2013
2265640  |1               |02/12/2013  |05/12/2013
2267461  |1               |02/12/2013  |05/12/2013
2273007  |1               |02/12/2013  |06/12/2013
2273127  |1               |02/12/2013  |06/12/2013

以下是打开文件并检索数据的表单代码部分......

Dim JDW_Row As String
Dim JDW_RowValues() As String

' Process JDW Order File into DataGrid

        Dim JDW_Reader As IO.StreamReader = New IO.StreamReader(File.OpenRead(FullPath), System.Text.Encoding.Default)

        ' Skip Header
        JDW_Reader.ReadLine()

        ' Start reading file contents
        Do Until JDW_Reader.EndOfStream

            ' read a record and split into fields

            JDW_Row = JDW_Reader.ReadLine()
            JDW_RowValues = Split(JDW_Row, "|")

        Loop

我得到了JDW_RowValues的每个成员,我需要问'我以前见过你'是通过将它写入数组并递增相关的计数器或数组值,如果它再次遇到但是我不确定代码在VB .NET中。

有人能建议最好的方法吗?

2 个答案:

答案 0 :(得分:0)

您可以为您的欺骗数据创建一个对象,并通过List(of YourObject)跟踪计数。

以下是一个例子:

Public Class DupedOrderData

    Public Property OrderID As Integer
    Public Property LinesOnOrder As Integer
    Public Property OrderDate As Date
    Public Property ExpectedDate As Date

End Class

然后在你的循环之前:

    Dim lstOrders As New List(Of DupedOrderData)

然后在你的循环中:

    Dim JDW_ROW As List(Of String) = JDW_Reader.ReadLine().Split("|").ToList()

    'Already in List (LINQ Query)?
    Dim objOrder As DupedOrderData = (From objTarget As DupedOrderData In lstOrders _
                                      Where objTarget.OrderID = JDW_ROW(0)).FirstOrDefault()

    If objOrder Is Nothing Then

        'Add to List
        objOrder = New DupedOrderData

        With objOrder

            .ExpectedDate = CDate(JDW_ROW(6))
            .LinesOnOrder = 1
            .OrderDate = CDate(JDW_ROW(5))
            .OrderID = JDW_ROW(0)

        End With

        lstOrders.Add(objOrder)

    Else

        'Increment Count
        objOrder.LinesOnOrder += 1

    End If

答案 1 :(得分:0)

 Class OrderItem
    Public Property OrderNo As String  ' ???
    Public Property Quantity As Integer 
    Public Property OrderDate As DateTime
    Public Property ExpectedDate As DateTime

    Sub New(newOrderNo)
       OrderNo = newOrderNo
    End Sub
 End Class

 Friend colOrderItems as New Dictionary(Of String, OrderItem)

 ' ... pseudo for adding in the loop

 Dim item as OrderItem

 Do Until JDW_Reader.EndOfStream
     If colOrderItems.ContainsKey(newOrderIDRead) Then
         ' increment quatity
         colOrderItems(newOrderIDRead).Quantity += newQuanReadValue

     Else
        ' add a new one
        item = New OrderItem(newOrderIDRead)
        item.Quantity = newQuanReadValue
         ...
         colOrderItems.Add(newOrderIDRead, item)
     End If
 Loop 

字典有助于清除重复项,因为只允许该值的一个键(OrderID),并且很容易找到现有的键。 List(of OrderItem)也可以使用,但它本身并不存在欺骗(这也部分取决于欺骗的定义)。