使用linq vb.net比较n个自定义列表/集合

时间:2014-08-06 14:43:26

标签: vb.net linq-to-objects

我试图比较n个自定义列表并从每个列表中删除匹配的记录,即

Public Class User

    Protected _UserId As String
    Protected _UserName As String
    Protected _Email As String
    Protected _Address As String

    Public Property UserId() As String
        Get
            Return _UserId
        End Get
        Set(ByVal value As String)
            _UserId = value
        End Set
    End Property

    Public Property UserName() As String
        Get
            Return _UserName
        End Get
        Set(ByVal value As String)
            _UserName = value
        End Set
    End Property

    Public Property Email() As String
        Get
            Return _Email
        End Get
        Set(ByVal value As String)
            _Email = value
        End Set
    End Property

    Public Property Address() As String
        Get
            Return _Address
        End Get
        Set(ByVal value As String)
            _Address = value
        End Set
    End Property
End Class

现在我在另一个列表中有n个List(用户),所以它的列表(列表(用户)列表)

ListMain-List1 : 

UserId      UserName      Email             Address
1           ABC           abc@hello.com     test123


ListMain-List2 : 

UserId      UserName      Email             Address
1           ABC           abc@hello.com     test123


ListMain-List3 : 

UserId      UserName      Email             Address 
1           ABC           abc@hello.com     test123
2           PQR           pqr@hello.com     test123 
3           XYZ           xyz@hello.com     test123

最多列表n

现在我需要检查所有列表项目是否有计数。如果所有邮件列表项(MainUserList(0).UserList.Count = MainUserList(1).UserList.Count = MainUserList(2).UserList.Count)中的count相同,则将它们进行比较。

如果任何列表中的任何字段不同,则返回false /某个指示符。

我检查过Intersect / Expect / Join但是我不知道如何采用这种方法和设备这个算法,因为主列表中的项目没有n即变量因此如何循环它们并且它们可能是使用linq join来检查平等...

任何帮助将不胜感激......谢谢!

编辑:数据的o / p与计数不匹配的列表完全相同。

考虑以下数据集

ListMain-List1 : 

UserId      UserName      Email             Address
1           ABC           abc@hello.com     test123


ListMain-List2 : 

UserId      UserName      Email             Address
1           ABC           abc@hello.com     test123


ListMain-List3 : 

UserId      UserName      Email             Address 
1           ABC           abc@hello.com     test123

它应该表明它们是相同的并从所有列表中删除此记录

希望现在更清楚......

确切的结构如下所示:

MainList
    Child1
         UserInfo
             Users as List(of User)
    Child2
         UserInfo
             Users as List(of User)
    Child3
         UserInfo
             Users as List(of User)
    Child4
         UserInfo
             Users as List(of User)
    Childn
         UserInfo
             Users as List(of User)

修改了代码......

Dim IsUser As Boolean
IsUser = _userList.All(Function(x) x.UserInfo.Users.Count > 0)
Dim userCount As Integer = 0
Dim userGroupCount As Integer = 0


If (IsUser) Then

    Dim firstCount = _userList.First().UserInfo.Users.Count
    Dim allSameLength = _userList.All(Function(x) x.UserInfo.Users.Count = firstCount)

    Dim allSame = False
    If allSameLength Then
        ' Combine the lists, and group all the Users by key values
        Dim groups = _userList.SelectMany(Function(x) x.UserInfo.Users) _
            .GroupBy(Function(x) New With {Key x.UserId, Key x.UserName, Key x.Email, Key x.address}).ToList()

' Each group should have one item for each original list. If not, there
        ' are different `key`s in the lists (assuming there were no dups in
        ' the original lists).

        For Each objBase As Object In _userList
            userCount += objBase.UserInfo.Users.Count
        Next

        userGroupCount = groups.Count()

        Dim sameUsers = IIf(userCount = userGroupCount, True, False)

        If sameUsers Then
            ' All PMs in each group should be equal
            'allSame = groups.All(Function(x) x.All(Function(y) y.Equals(x.First())))
        End If
    End If

End If

但如果没有最终的结果,它仍然是不完整的......

1 个答案:

答案 0 :(得分:0)

如果我理解了你想要的内容,你可以检查MainUserList中的所有列表是否具有相同的长度,这只是检查所有列表的长度是否与第一个列表相同:

Dim firstCount = MainUserList.First().UserList.Count
Dim allSameLength = MainUserList.All(Function(x) x.UserList.Count = firstCount)

对于其他检查,我能想到的最好的方法是合并所有列表中的所有User,然后按UserId对它们进行分组。如果一切都相同,则每个组中User的数量应与原始列表的数量相同,并且每个组中的所有User应为Equal:< / p>

Dim allSame = False
If allSameLength Then
    ' Combine the lists, and group all the Users by ID
    Dim groups = MainUserList.SelectMany(Function(x) x.UserList) _
        .GroupBy(Function(x) x.UserId).ToList()
    ' Each group should have one item for each original list. If not, there
    ' are different `UserId`s in the lists (assuming there were no dups in
    ' the original lists).
    Dim sameUsers = groups.All(Function(x) x.Count = MainUserList.Count)
    If sameUsers Then
        ' All Users in each group should be equal
        allSame = groups.All(Function(x) x.All(Function(y) y.Equals(x.First())))
    End If
End If

为了实现此目的,您需要覆盖Equals课程中的User方法,或更改上述比较:

Public Overrides Function Equals(other As Object) As Boolean
    If other Is Me Then
        Return True
    End If
    Dim otherUser = TryCast(other, User)
    If otherUser Is Nothing Then
        Return False
    End If
    Return Me.UserId = otherUser.UserId _
        AndAlso Me.UserName = otherUser.UserName _
        AndAlso Me.Email = otherUser.Email _
        AndAlso Me.Address = otherUser.Address
End Function