tags:

views:

22

answers:

1

I have an observable collection ie Owners and that collection has child items ie Dogs.

Now given another collection of local Dogs how can I select all the Owners where there Dogs exist in my local Dogs collection. The equality condition would be that the Dog == Dog.

A: 

The following line should do the trick. This will give you all owners where at least one of their dogs is in the local list.

owners.Where(owner => owner.Dogs.Any(dog => localDogs.Contains(dog)))

If you only want the owners where all their dogs are in the local list, then use the following.

owners.Where(owner => owner.Dogs.All(dog => localDogs.Contains(dog)))

The tiny difference is Any()vs. All().

Daniel Brückner
Wow. Brilliant one-liner. Elegantly simple. Thanks. To think I was going to do some nested for loops. I also realize my question wasn't completely clear, but you inferred the correct meaning.
tim