nhibernate criteria for selecting from different tables

前端 未结 1 1501
礼貌的吻别
礼貌的吻别 2021-01-27 04:48

I have following table model:

\"enter

I want following SQL command as nhibernate c

相关标签:
1条回答
  • 2021-01-27 05:03

    If you want the last five orders I would approach the problem with a slightly different query

    Select * 
    From order o join Units U on O.OID = U.OID
    Order by O.PONumber limit 5
    

    in nhibernate would be something like that (not tested)

    Order orderAlias = null;
    Unit unitAlias = null;
    var query = session.QueryOver<Order>(() => orderAlias)
       .JoinAlias(() => orderAlias.Units, () => unitAlias, JoinType.InnerJoin)
       //.TransformUsing(Transformers.DistinctRootEntity) if you have duplicates
       .OrderBy(x => x.PONumber).Desc.Take(5);
    

    UPDATE

    You could also just load Order entity. NHibernate will load automatically all units rows, based on your mapping info (lazy/eager loading or joins).

    var query = session.QueryOver<Order>().OrderBy(x => x.PONumber).Desc.Take(5);
    
    0 讨论(0)
提交回复
热议问题