List child records not linked to a given parent

Richard

What's the best way to find all child records who are not linked to a certain parent record via a many-to-many relationship? They can have any other parent, or none at all.

I'm trying things like this:

children = db.Children
  .Where(c => !c.Parents
     .Where(x => x.parentId == knownParentId)
     .Any())
  .ToList();

...it returns one record that I'm interested in but not others that I know exist (probably using 'Any' when I shouldn't be)

Thanks

octavioccl

You can use All extension method as follow:

children = db.Children.Where(c => c.Parents.All(x => x.parentId != knownParentId)).ToList();

Or you can still use Any extension method:

children = db.Children.Where(c => !c.Parents.Any(x => x.parentId == knownParentId)).ToList();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysql find parent records where given values are found in all child records

From Dev

Include child records count in the parent record attributes list without fetching all child records

From Dev

Is it possible to find a given child class in a list of parent classes in C#?

From Dev

Given a flat list of (parent,child), create a hierarchical dictionary tree

From Java

Hibernate: How to make Hibernate delete records from child table when deleting parent if child is linked to parent with many-to-one?

From Dev

Search through child records fields of given parent using hibernate lucene search

From Dev

Duplicating parent, child and grandchild records

From Dev

Delete Child Records with Missing Parent

From Dev

Select Parent and Child if child has records

From Dev

Order a parent/child list

From Dev

How to get all records as parent records with child records chained together?

From Dev

Reading child list of a parent list

From Dev

adding information to child->child LINKED LIST

From Dev

SQL - select parent and child records in an order

From Dev

retrieve parent child records in one query

From Dev

Odoo - Filter child records in parent class

From Dev

Rails index method display parent with child records

From Dev

Query parent relation when querying for child records

From Dev

Azure search return parent and child records

From Dev

Rails group_by query with parent & child records

From Dev

Flat database records to parent Child XML transformation

From Dev

Expand parent prefix number to a given child number

From Dev

keep only parent and corresponding child rows linked by parent case ID

From Dev

MySQL - Return parent records based on conflicting WHERE clauses on child records

From Dev

Filtering out parent records based on conditions on child records

From Dev

Rails scope for parent records that do not have particular child records

From Dev

Display Parent Records After Related Child Records in SQL Server

From Dev

f# linked list implementing with records

From Dev

Order list by parent and child items

Related Related

  1. 1

    Mysql find parent records where given values are found in all child records

  2. 2

    Include child records count in the parent record attributes list without fetching all child records

  3. 3

    Is it possible to find a given child class in a list of parent classes in C#?

  4. 4

    Given a flat list of (parent,child), create a hierarchical dictionary tree

  5. 5

    Hibernate: How to make Hibernate delete records from child table when deleting parent if child is linked to parent with many-to-one?

  6. 6

    Search through child records fields of given parent using hibernate lucene search

  7. 7

    Duplicating parent, child and grandchild records

  8. 8

    Delete Child Records with Missing Parent

  9. 9

    Select Parent and Child if child has records

  10. 10

    Order a parent/child list

  11. 11

    How to get all records as parent records with child records chained together?

  12. 12

    Reading child list of a parent list

  13. 13

    adding information to child->child LINKED LIST

  14. 14

    SQL - select parent and child records in an order

  15. 15

    retrieve parent child records in one query

  16. 16

    Odoo - Filter child records in parent class

  17. 17

    Rails index method display parent with child records

  18. 18

    Query parent relation when querying for child records

  19. 19

    Azure search return parent and child records

  20. 20

    Rails group_by query with parent & child records

  21. 21

    Flat database records to parent Child XML transformation

  22. 22

    Expand parent prefix number to a given child number

  23. 23

    keep only parent and corresponding child rows linked by parent case ID

  24. 24

    MySQL - Return parent records based on conflicting WHERE clauses on child records

  25. 25

    Filtering out parent records based on conditions on child records

  26. 26

    Rails scope for parent records that do not have particular child records

  27. 27

    Display Parent Records After Related Child Records in SQL Server

  28. 28

    f# linked list implementing with records

  29. 29

    Order list by parent and child items

HotTag

Archive