2012-12-11 128 views
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() 

「部分不工作,它帶來的發票數量與數字超過100,而不是重複發票號:

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

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

回答

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 
+0

這是完美的,它工作正常,非常感謝你! – Fernando

+0

非常歡迎。快樂編程! – SSS

0

您需要使用的GroupBy()函數。 VB不是我的語言,所以我會盡我所能翻譯。

Dim invoice = From c In pesquisanotas 
       Group c By c.number Into g 
       Where (g.Count > 1) 
       Select g.Key 
+0

你好,非常感謝,但我得到以下錯誤:錯誤方法'G'的定義不可訪問我在這種情況下。 – Fernando

+0

我試圖用「進入組」替換「進入組」,但它沒有工作...我是否需要將某些東西導入到我的項目中? – Fernando

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 
+0

非常感謝,但我需要保留重複的項目,我只需要在一個單獨的列表中標識它們... – Fernando