如何根据条件返回已连接的对象?

时间:2010-07-07 11:39:52

标签: nhibernate criteria

我想知道如何在条件API中表示以下内容

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

where子句是直截了当的,我正在绊倒的是如何将连接对象作为结果?

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

这是我的尝试,因为它返回错误的类型而无效。

2 个答案:

答案 0 :(得分:3)

试试这个:

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>();

答案 1 :(得分:0)

我有以下工作,我不知道它是否会比使用投影更好或更差?

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>();
相关问题