且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LINQ VB如何检查对象列表中的重复项

更新时间:2023-11-28 23:12:46

项目在后面,我只是这样将其砍死:

The project is behind, I just hacked it together like this:

    ' For each outcome, if it is in the list of valid outcomes more than once, and it is not in the list of 
    ' duplicates, add it to the duplicates list.
    Dim lstDuplicates As New List(Of objectType)
    For Each outcome As objectType In lstOutcomes
        'declare a stable outcome variable
        Dim loutcome As objectType = outcome
        If lstOutcomes.Where(Function(o) o.ID = loutcome.ID).Count > 1 _
        AndAlso Not lstDuplicates.Where(Function(d) d.ID = loutcome.ID).Count > 0 Then
            lstDuplicates.Add(outcome)
        End If
    Next
    If lstDuplicates.Count > 0 Then
        valid = False
        sbErrors.Append("There cannot be multiple outcomes of any kind. The following " & lstDuplicates.Count & _
                        " outcomes are duplicates: ")
        For Each dup As objectType In lstDuplicates
            sbErrors.Append("""" & dup.Name & """" & " ")
        Next
        sbErrors.Append("." & vbNewLine)
    End If