我在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
答案 0 :(得分: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
答案 1 :(得分:0)
您需要使用GroupBy()函数。 VB不是我的语言,所以我会尽力翻译。
Dim invoice = From c In pesquisanotas
Group c By c.number Into g
Where (g.Count > 1)
Select g.Key
答案 2 :(得分: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