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
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.
Comments