Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting NSPredicate: Matching "ANY" that match two conditions

**I'm a bit at a loss about how to format an NSPredicate where "any" of a certain relationship matches more than one condition.

So for example, say I'm doing a fetch request for an "Employer" entity and I want to get all employers that have at least one employee under age 18. So, I can use "ANY employees.age < 18" - simple enough. But say I want to match any that have an employee under 18 who also has the last name "Howser." That is, it's not enough for the employer to match both "any employee under 18" and "any employee named 'Howser'" - they have to have at least one employee that matches both conditions.

How would this predicate be formatted?

like image 553
Ascendant Avatar asked Aug 17 '11 09:08

Ascendant


1 Answers

This is one of the rare circumstances where you need SUBQUERY:

SUBQUERY(employees, $e, $e.age < 18 && $e.lastName == 'Howser').@count > 0
like image 158
Dave DeLong Avatar answered Oct 23 '22 04:10

Dave DeLong