857

LINQ to Objects - Does Not Contain?

I have a collection of Items that each have a collection of Relationships. I have a list of Groups that Items can have Relationships with.

I can find all the Items that have a particular relationship but I now want to find all the Items that don't have a Relationship with any of my Groups.

I can find the Items that have a relationship with any of the Groups by doing this:

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

How can I find all the items that do not have a relationship with any of the groups?

Answer1:

I don't remember VB all that well, but a simple "Not" should work.

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

Answer2:

Have you tried negating the results of the Contains method?

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

Answer3:

If you're generating the haveGroup collection anyway then you could just do something like this:

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

Answer4:

Dim notHasGroup = items.Except(haveGroup)

Answer5:

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

Recommend

  • Display a record on subform based on form's combo box selection (using VBA)
  • Get id's of a ManyToMany mapping table
  • How do I organize such database in SQLite?
  • Architecture for communication between two microservices
  • Can I create a separate table for a single Collection property using Entity Framework code first?
  • Ruby error (uninitialized constant User::Relationship)
  • UML Class Diagram construction
  • SelfReferenceProperty question
  • Core data structure use multiple entities or not?
  • How can I join with Eloquent: Relationships?
  • E2L: How do you handle Foreign Keys that participate in multiple relationships?
  • Cleaner way of handling addition of summarizing rows to table?
  • Silverstripe - filter DataObject list by many many relationship
  • Multiple Many-to-Many Relationships (Circular Relationship)
  • Java JTable: Setting a primary column to always sort by first
  • Angular Firestore: What is the correct syntax for a collection query that uses a where clause?
  • How do I do two has_many/belongs_to relationships between two models?
  • XNA Number lock affects input
  • Best modern way of creating indentation in a ?
  • How to extend Relationship class in neo4django
  • Injecting service into a mixin Ember2.3+
  • Neo4j: Legacy Indexes and auto index vs new label bases schema indexes
  • Excel VBA URLDownloadToFile Error for https Ressource
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • Creating a Multidimensional, Associative Array in VBScript
  • Z3: Convert between FP and BitVector?
  • Conversion from string “a” to type 'Boolean' is not valid
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Counter field in MS Access, how to generate?
  • Convert array of 8 bytes to signed long in C++
  • How to get next/previous record number?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do you join a server to an Active Directory (domain)?
  • How to stop GridView from loading again when I press back button?
  • Turn off referential integrity in Derby? is it possible?
  • How does Linux kernel interrupt the application?
  • Add sale price programmatically to product variations
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?