2

我有一个项目集合,每个项目都有一个关系集合。我有一个项目可以与之建立关系的组列表。

我可以找到所有具有特定关系的项目,但我现在想找到与我的任何组没有关系的所有项目。

我可以通过执行以下操作找到与任何组有关系的项目:

Dim groupIds as List(of Integer) = (From g In cmdbGroups Select g.ID).ToList
Dim haveGroup = (From item In items _
                 Where item.Relationships.Any(Function(r) groupIds.Contains(r.TargetID)) _
                 Select item).ToList

如何找到与任何组没有关系的所有项目?

4

5 回答 5

6

您是否尝试过否定该Contains方法的结果?

Dim groupIds as List(of Integer) = (From g In cmdbGroups Select g.ID).ToList
Dim haveGroup = (From item In items _
                 Where item.Relationships.Any(Function(r) Not groupIds.Contains(r.TargetID)) _
                 Select item).ToList
于 2009-06-11T16:52:16.567 回答
6

我不太记得 VB,但是一个简单的“不”应该可以工作。

Dim groupIds as List(of Integer) = (From g In cmdbGroups Select g.ID).ToList
Dim haveGroup = (From item In items _
             Where Not item.Relationships.Any(Function(r) groupIds.Contains(r.TargetID)) _
             Select item).ToList
于 2009-06-11T16:53:50.417 回答
2

如果您haveGroup无论如何都要生成集合,那么您可以执行以下操作:

Dim groupIds as List(of Integer) = (From g In cmdbGroups Select g.ID).ToList

Dim haveGroup = (From item In items _
    Where item.Relationships.Any(Function(r) groupIds.Contains(r.TargetID)) _
    Select item).ToList

Dim haveNotGroup = items.Except(haveGroup).ToList
于 2009-06-11T16:58:04.320 回答
2
Dim notHasGroup = items.Except(haveGroup)
于 2009-06-28T10:34:29.433 回答
0
Dim listIds as List(of Integer) = (From g In cmdbGroups Select g.ID).ToList
Dim haveGroup = (From item In items _
    Where Not listIds.Contains(item.ID) 
    Select item.ID).ToList
于 2016-05-16T18:00:31.477 回答