The easiest Criteria query would be:

var peopleWithFords = session.CreateCriteria<Person>()
.CreateCriteria("Cars")
.Add(Restrictions.Eq("Make", "Ford"))
.SetResultTransformer(Transformers.DistinctRootEntity)
.List<Person>();

However, that'll issue an additional select to get people's cars (it's
transparent to the code, but you have created a SELECT N+1 problem).

This query will do exactly what you want:

var peopleWithFordsIds = DetachedCriteria.For<Person>()
.CreateAlias("Cars", "car")
.Add(Restrictions.Eq("car.Make", "Ford"))
.SetProjection(Projections.Property("Id"));
var peopleWithFords = session.CreateCriteria<Person>()
.Add(Subqueries.PropertyIn("Id", peopleWithFordsIds))
.SetFetchMode("Cars", FetchMode.Join)
.SetResultTransformer(Transformers.DistinctRootEntity)
.List<Person>();

BTW, I renamed your People class to Person :-)

   Diego


On Mon, Dec 21, 2009 at 08:27, Eric J. Peters <[email protected]> wrote:

>
> This seems like it should be a simple query, but I just can't figure
> it out.
>
> I want a criterion that represents all People who own at least 1 Ford,
> and I want the results to return all Cars those people own.
>
> People { int Id, IList<Car> Cars }
> Car { int Id, string make, string model }
>
> I was hoping for an Expression.Contains("Cars", carObject), or even
> better yet Expression.Contains("Cars.make", "Ford"), but clearly
> neither exist.  Everything I've tried seems to either give me runtime
> errors or no results.  Can somebody point me in the right direction?
>
> Thanks,
> -Eric.
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
>

--

You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.


Reply via email to