有人可以给我一个关于如何将这个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
where clientId=clients.Id
)
我无法弄清楚where子句中的最后一个术语。提前谢谢。
答案 0 :(得分:0)
尝试coalesce
函数,它是SQL查询中nvl
函数的模拟函数。因此,在您的情况下,WHERE
中的最后一个字词将如下所示
coalesce(clientOrders.DateCreated, clientOrders.DateCreated=(select MAX(DateCreated) from clientOrders where clientId=clients.Id))
希望有所帮助
答案 1 :(得分:0)
没有关于映射的更多信息将无法获得正确的代码,但它将是这样的:
假设您的客户中有一组客户订单,名为'ClientOrders'
//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>();
同样,这应该只给你一个提示,因为没有你的映射,就不可能知道你在那里有什么。希望它有所帮助...