0

有人可以给我一个关于如何将此 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 子句中的最后一个术语。提前致谢。

4

2 回答 2

0

试试这个coalesce函数,它类似于nvlSQL 查询中的函数。所以,在你的情况下,最后一个词WHERE看起来像这样

coalesce(clientOrders.DateCreated, clientOrders.DateCreated=(select MAX(DateCreated) from clientOrders where clientId=clients.Id))

希望能帮助到你

于 2013-06-24T19:33:15.857 回答
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>();

同样,这应该只给你一个提示,因为没有你的映射,就不可能知道你在那里有什么。希望能帮助到你...

于 2013-06-24T21:18:31.720 回答