比较和合并对象列表

时间:2015-02-03 09:08:12

标签: vb.net linq

我有一个列表,其中包含许多具有此定义的对象:

Public Class Helper
    Public Property UserPrincipal As UserPrincipal
    Public Property Groups As New List(Of String)
    Public Property Server As New List(Of String)
End Class

假设,一个对象看起来像这样:

UserPrincipal.Name = Jan
Groups = {group_1, group_2, group_3}
Server = {Server1}

还有一个:

UserPrincipal.Name = Jan
Groups = {group_1, group_3}
Server = {Server2}

现在我要检查物业"群组"如果"组"每个对象并创建一个新对象一个对象包含" Groups"另一个对象。

所以新的对象列表应如下所示:

UserPrincipal.Name = Jan
Groups = {group_1, group_2, group_3}
Server = {Server1, Server2}

使用linq可以吗?

谢谢和问候, 扬

更新:10:42:从字符串到字段(字符串)更改了' Server' -property的类型

更新12:04:让我试着澄清一下我的问题。 任务是收集服务器的本地组的成员。为此,我使用具有正确凭据的新principalcontext连接到每个服务器,获取正确的组(它是远程桌面用户组)并获取该组的所有成员。 有了这些信息,我填写了提到的帮助对象 - 包含userprincipal(该组的成员),组(远程桌面用户组的成员)和服务器名称。

所以我得到了n * Helper-objects,其中n是服务器数。

现在,有两个要求:我们说,我在这里有两台服务器,server1server2。一切都等于但服务器名称不同,所以我只想要一个具有属性Server = {server1, server2}的对象。

第二个要求基本上与第一个要求相似但是:如果Groups属性包含至少一个唯一关联,并将此组添加到列表中,如果它尚未包含在列表中,请执行此操作

现在不知道它是否更清楚:)现在将展示一个简短的例子。

对象1:
    UserPrincipal.Name = Jan     群组= {域名用户}
    服务器= {Server1}

对象2:
    UserPrincipal.Name = Jan     群组= {域名用户}
    服务器= {Server2}

预期对象:
    UserPrincipal.Name = Jan     群组= {域名用户}
    Server = {Server1,Server2}

例2:
对象1:
    UserPrincipal.Name = Jan     Groups = {Domain-Users,Test-Users}
    服务器= {Server1}

对象2:
    UserPrincipal.Name = Jan     Groups = {Test-Users}
    服务器= {Server2}

预期对象:
    UserPrincipal.Name = Jan     Groups = {Domain-Users,Test-Users}
    Server = {Server1,Server2}

最后一个,嗯:

对象1:
    UserPrincpial.Name = Jan     Groups = {Test-Users}
    服务器= {Server1}

对象2:
    UserPrincipal.Name = Jan     群组= {域名用户}
    Server = {Server1}或{Server2}等并不重要。

预期结果:无变化原因属性groups完全不同。

1 个答案:

答案 0 :(得分:0)

根据您更新的问题和您的意见,这似乎是您想要的:

Dim upGroups = From helper In helpers 
               Group helper By helper.UserPrincipal Into Group
               Select New Helper With {
                 .UserPrincipal = UserPrincipal,
                 .Groups = Group.SelectMany(Function(h) h.Groups).Distinct().ToList(),
                 .Server = Group.SelectMany(Function(h) h.Server).Distinct().ToList()
               }
Dim newHelpers = upGroups.ToList()

旧答案

您可以使用Groups.Intersect(h.Groups).Any()检查哪些助手包含相交的组。然后你可以使用循环来合并它们:

Dim query = From helper In helpers
            Let group = helpers.Where(Function(h) helper.Groups.Intersect(h.Groups).Any())

Dim processed As New HashSet(Of Helper)()
Dim newHelpers As New List(Of Helper)
For Each x In query
    If x.group.Any(AddressOf processed.Contains) Then
        ' was already processed, skip '
        Continue For
    End If
    Dim mergedHelper = New Helper With {
               .UserPrincipal = x.group.First().UserPrincipal,
               .Groups = x.group.SelectMany(Function(h) h.Groups).Distinct().ToList(),
               .Server = x.group.SelectMany(Function(h) h.Server).Distinct().ToList()
           }
    newHelpers.Add(mergedHelper)
    For Each helperInGroup In x.group
        processed.Add(helperInGroup)
    Next
Next