LINQ to Objects - 不包含?

时间:2009-06-11 16:49:17

标签: vb.net linq linq-to-objects

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

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

我可以通过这样做找到与任何组有关系的项目:

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

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

5 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分:2)

Dim notHasGroup = items.Except(haveGroup)

答案 4 :(得分: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