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

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 に答える
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 に答える