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

マッピングに関する詳細情報がなければ、正しいコードを取得することはできませんが、次のようになります。

クライアントに「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 に答える