本文介绍了流利的NHibernate - 过滤结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FNH新手问题 -

我有两个对象RR和TT定义如下:

$ pre > public class RR
{
Id
Name
TT_Id //对应TT.Id
}

公共类TT
{
Id
名称
类型
}

我想根据以下SQL过滤来自RR的记录:

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

任何人都可以请指导如何在NH中添加过滤条件来达到上述目的。



谢谢!

解决方案

为什么你有 TT_Id 而不是 TT 本身在 RR 中?如果它引用 TT ,那么

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

否则

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

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

 的IList<对象[] GT; 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你自己在这里


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!

解决方案

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

这篇关于流利的NHibernate - 过滤结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 16:50