3

我在 VB.NET 中有一个代码,我需要在发票列表中找到重复的项目,具有相同的编号(金额可能不同),这是我必须做的但不起作用,我不知道我是否也许应该使用 LINQ...数据库在 txt 文件中。

Public Class Invoice
  Public Property amount As Decimal
  Public Property number As String
  Public Property date As Date

  'extracting information from txt file

  Dim pesquisanotas As List(Of Invoice) = New NotaRepositorio().research()

'部分不工作,它带来的是数字超过 1 的发票号码,而不是重复的发票号码:

Dim DuplicateInvoice= From c In pesquisanotas
                                  Where (c.number.Count > 1)
                                  Select c

                    For Each item In DuplicateInvoice
                        MessageBox.Show(item.number)
                    Next
4

3 回答 3

1

如果重复项已经在列表中,那么您可以通过以下方式检测它们:

Class DemoClass
  Public Property amount As Decimal
  Public Property number As String
  Public Property [date] As Date
End Class

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  Dim lstSource As New List(Of DemoClass)
  lstSource.Add(New DemoClass With {.amount = 121, .number = "121", .date = #1/1/2012#})
  lstSource.Add(New DemoClass With {.amount = 122, .number = "122", .date = #1/2/2012#})
  lstSource.Add(New DemoClass With {.amount = 123, .number = "123", .date = #1/3/2012#})
  lstSource.Add(New DemoClass With {.amount = 124, .number = "124", .date = #1/4/2012#})
  lstSource.Add(New DemoClass With {.amount = 125, .number = "125", .date = #1/5/2012#})
  lstSource.Add(New DemoClass With {.amount = 126, .number = "126", .date = #1/6/2012#})
  lstSource.Add(New DemoClass With {.amount = 127, .number = "127", .date = #1/7/2012#})
  lstSource.Add(New DemoClass With {.amount = 128, .number = "123", .date = #1/8/2012#}) 'dup'
  lstSource.Add(New DemoClass With {.amount = 129, .number = "129", .date = #1/9/2012#})
  lstSource.Add(New DemoClass With {.amount = 130, .number = "130", .date = #1/10/2012#})
  lstSource.Add(New DemoClass With {.amount = 131, .number = "122", .date = #1/11/2012#}) 'dup'
  lstSource.Add(New DemoClass With {.amount = 132, .number = "132", .date = #1/12/2012#})

  For intOuter As Integer = 0 To lstSource.Count - 2
    For intInner As Integer = intOuter + 1 To lstSource.Count - 1
      If lstSource(intOuter).number = lstSource(intInner).number Then
        'duplicate found
        MsgBox("Duplicate found: " & lstSource(intOuter).number)
      End If
    Next intInner
  Next intOuter
End Sub
于 2012-12-12T04:11:57.983 回答
0

实现一个自定义IEquatable(Of T)并使用该List(Of T).Contains函数来防止添加重复项:

Class DemoClass
  Implements IEquatable(Of DemoClass)

  Public Property amount As Decimal
  Public Property number As String
  Public Property [date] As Date

  Public Function Equals1(other As DemoClass) As Boolean Implements System.IEquatable(Of DemoClass).Equals
    If Me.number = other.number Then
      Return True
    Else
      Return False
    End If
  End Function
End Class

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  Dim lst As New List(Of DemoClass)
  Dim dc1 As New DemoClass With {.amount = 123, .number = "123", .date = #1/2/2012#}
  Dim dc2 As New DemoClass With {.amount = 124, .number = "124", .date = #1/3/2012#}
  Dim dc3 As New DemoClass With {.amount = 123, .number = "123", .date = #1/2/2012#} 'same as dc1!!
  If Not lst.Contains(dc1) Then lst.Add(dc1)
  If Not lst.Contains(dc2) Then lst.Add(dc2)
  If Not lst.Contains(dc2) Then lst.Add(dc3) 'this won't get added!'
  MsgBox(lst.Count) 'should return 2'
End Sub
于 2012-12-11T03:19:35.350 回答
0

您需要使用 GroupBy() 函数。VB不是我的语言,所以我会尽力翻译。

Dim invoice = From c In pesquisanotas
                Group c By c.number Into g
                Where (g.Count > 1)
                Select g.Key
于 2012-12-11T02:05:46.193 回答