views:

28

answers:

2

Hi, I'm wondering how I can represent the following in the criteria API

return DataContext.Session.CreateQuery("
    select
        ss.PracticeArea 
    from Subsection as ss
    where ss.Location = :Location
    ")
    .SetEntity("Location", location)
    .List<PracticeArea>();

The where clause is straight forward enough the bit I'm tripping over is how to get the joined object as the result ?

DataContext.Session.CreateCriteria<Subsection>()
    .Add(Restrictions.Eq("Location", location))
    .List<PracticeArea>();

This is my attempt which doesnt work since it returns the wrong type.

+3  A: 

Try this:

DataContext.Session
    .CreateCriteria<Subsection>()
    .CreateCriteria("Subsecion", "ss")
    // the "select" clause is called "projection"
    .SetProjection(Projections.Property("ss.PracticeArea"))
    .Add(Restrictions.Eq("Location", location))
    .List<PracticeArea>();
Stefan Steinegger
Looks like the kind of direction I should be heading in.However it seems to be getting the practice areas in subsequent selects rather than in one hit.Sorry I dont think it was clear that Location and PracticeArea are entities.One thing which is confusing me is the second CreateCriteria ?Thanks in advance.
Saint Gerbil
The second CreateCriteria is just the join. You could try without it. If it performs too many selects, try `.SetFetchMode(ss.PracticeArea, FetchMode.Join)`
Stefan Steinegger
Thanks that's got it going.
Saint Gerbil
A: 

I've got the following to work, I don't know if its going to perform better or worse than using a projection?

DetachedCriteria subsections = DetachedCriteria.For<Subsection>()
    .SetProjection(Projections.Property("PracticeArea.Id"))
    .Add(Restrictions.Eq("Location", location));

return DataContext.Session
    .CreateCriteria<PracticeArea>()
    .Add(Subqueries.PropertyIn("Id",subsections))
    .List<PracticeArea>();
Saint Gerbil