Fluent NHibernate - Filtering resultset

孤街醉人 提交于 2020-01-16 14:35:09

问题


FNH Newbie question -

I have two objects RR and TT defined as following:

public class RR
{
   Id
   Name
   TT_Id //corresponds to TT.Id
}

public class TT
{
   Id
   Name
   Type   
}

I want to filter records from RR as per following SQL:

SELECT RR.*
FROM RR 
LEFT OUTER JOIN TT ON RR.TT_Id = TT.Id
WHERE TT.Type <> 7 

Can anyone please guide how can I add filter criteria to achieve above in NH.

Thank you!


回答1:


why you have TT_Id and not TT itself in RR? IMO it will make a lot of things more difficult than needed.

if it would reference to TT then

resultlist = session.CreateCriteria<RR>()
    .CreateCriteria("TT")
    .Add(Restrictions.Not(Restrictions.Eq("Type", 7)))
    .List<RR>();

otherwise

var subquery = DetachedCriteria.For<TT>()
    .Add(Restrictions.Not(Restrictions.Eq("Type", 7)))
    .SetProjection(Projections.Id());

resultlist = session.CreateCriteria<RR>()
    .Add(Subqueries.In("TT_id", subquery)))
    .List<RR>();

or

IList<object[]> resultlist = session.CreateSqlQuery("SELECT RR.* FROM RR LEFT OUTER JOIN TT ON R.TT_Id = TT.Id WHERE TT.Type <> 7 ")
    .List().Cast<object[]>();

// hydrate RR yourself here


来源:https://stackoverflow.com/questions/6792895/fluent-nhibernate-filtering-resultset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!