nhibernate从1:m关系中选择顶级记录
有人能给我一个关于如何将这个SQL转换成NHibernate等价物的提示吗nhibernate从1:m关系中选择顶级记录,nhibernate,Nhibernate,有人能给我一个关于如何将这个SQL转换成NHibernate等价物的提示吗 select * from clients left join clientOrders on (clientOrders.clientId = clients.Id) where clientOrders.DateCreated is null or clientOrders.DateCreated =( select MAX(DateCreated) from clientOrders
select *
from clients
left join clientOrders
on (clientOrders.clientId = clients.Id)
where clientOrders.DateCreated is null
or clientOrders.DateCreated =(
select MAX(DateCreated)
from clientOrders
where clientId=clients.Id
)
我想不出where子句中的最后一个术语。提前感谢。尝试使用
coalesce
函数,它模拟了SQL查询中的nvl
函数。因此,在您的例子中,中的最后一个术语将如下所示
coalesce(clientOrders.DateCreated, clientOrders.DateCreated=(select MAX(DateCreated) from clientOrders where clientId=clients.Id))
希望它有帮助如果没有有关映射的更多信息,将不可能获得正确的代码,但可能是这样的:
假设您的客户中有一组客户订单,名为“客户订单”
//Create the criteria for objects of type 'Client'
ICriteria target = Session.CreateCriteria<Client>();
//create an alias for the client orders, to be able to add the restrictions
target.CreateAlias("Orders", "ClientOrders", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
//Add the restrinctions using an 'Or' to allow any of this two conditions
target.Add(Restrictions.Or(Restrictions.IsNull("Orders.DateCreated"), Restrictions.Eq("Orders.DateCreated",
Session.CreateCriteria<DateTime>().SetProjection(Projections.Max("Orders.DateCreated")).UniqueResult<DateTime>())));
//Get the list of the clients
target.List<Client>();
//为“Client”类型的对象创建条件
ICriteria target=Session.CreateCriteria();
//为客户订单创建别名,以便能够添加限制
CreateAlias(“Orders”、“ClientOrders”、NHibernate.SqlCommand.JoinType.LeftOuterJoin);
//使用“或”添加限制,以允许这两种情况中的任何一种
target.Add(Restrictions.Or(Restrictions.IsNull(“Orders.DateCreated”)、Restrictions.Eq(“Orders.DateCreated”),
Session.CreateCriteria().SetProjection(Projections.Max(“Orders.DateCreated”).UniqueResult());
//获取客户列表
target.List();
同样,这应该只给您一个提示,因为如果没有映射,就不可能知道其中有什么。希望它能帮上忙……到目前为止你有什么?还有一个非常类似的问题:不知道如何使用它。我期待的是:criteria.Add(Restrictions…,因为这是where子句的等价项。这看起来更像它。我将尝试一下,尽管我创建了一个视图并在映射中使用了它。感谢Sergio的帮助