[CB 3.5.1-7] Need Some Criteria Builder Foo

Hi All–

I’m hoping some of the criteria builder foo masters can help me out a bit. Let’s say I have these entities–Car, Part, Color.

Car has many Parts -> one-to-many from Car to Parts
Parts have a Color -> many to one from Parts to Colors

What I’m wanting to do is to retrieve a distinct array of Cars that have green parts (any kind), or perhaps an array of Cars that have white and blue parts. I don’t really care about the parts, just the existence of at least one part for the specified color(s). So in other words, if Car A has 15 red parts, I just want to get back the one Car A.

When I drill down via createAlias to the ColorID, the appropriate results are returned; however, it is the complete joined set (e.g., one “row” for part which matched the color), rather than simply the cars whose parts match the criteria.

Here’s an example of what I’ve tried:

var c = newCriteria();
c.createAlias( “Parts”, “p”).createAlias( “p.Colors”, “c” ).isIn( “c.ColorID”, theArrayOfColorIDs );

I’ve tried a number of other approaches, but each has given me the same result.

Can anyone suggest tweaks (or rewrites) to help me to get to what I need? I appreciate any help that anyone can offer.


Try a distinct restriction

Hi Luis–

Could you expand on this a bit? I don’t see a distinct restriction (unless you’re talking about the distinct projection?) in the docs.


Sorry yes. Projection

The project worked, but I wasn’t quite able to get it to work for what I was doing. I opted to do a calculated property on the entity itself.

Thanks for the guidance, though!