使用JoinQueryOver的NHibernate QueryOver选择所有列

时间:2013-02-01 14:08:52

标签: nhibernate queryover

我有一些带有多个连接的QueryOver,我得到的结果就返回的对象而言是可以的。这是代码>

   var l = session.QueryOver<Discount>(() => discount)
                .JoinQueryOver<ConPrdGrp>(r => r.ConPrdGrps)
                .JoinQueryOver<PrdGroupTree>(k => k.PrdGroupTree)
                .JoinQueryOver<Product>(g => g.Products)
                .Where(p => p.ProductID == Product.ProductID)
                .And(() => discount.FomDato <= DateTime.Now && discount.TomDato >= DateTime.Now).List();

但是,如果我查看SQL语句,我可以看到生成的查询选择了所有连接表中的所有列,尽管结果只返回Discount对象列表。我可以使用投影获得折扣的一些属性,查询将会小得多。但是我如何指示NHibernate只从Discount表中获取列而不是从所有连接表中获取列?

1 个答案:

答案 0 :(得分:3)

据我所知,有一种简单的方式:.Select(Projections.Properties<Discount>())。检查一下这个过时的问题(NHIbernate: Shortcut for projecting all properties?

您可以做的是明确命名您想要查看的列:

  ... // the QueryOver you have
  .SelectList (l => l
    .Select(() => discount.ID).WithAlias(() => discount.ID)
    .Select(() => discount.Code).WithAlias(() => discount.Code)
    ...
  )
  .TransformUsing(Transformers.AliasToBean<Discount>())
  ...

第二种方法可能是创建子查询,然后只是QueryOver<Discount>() 16.8. Subqueries

QueryOver<Discount> subQuery =
    QueryOver.Of<Discount>(() => innerDiscount)
      .JoinQueryOver<ConPrdGrp>(r => r.ConPrdGrps)
      .JoinQueryOver<PrdGroupTree>(k => k.PrdGroupTree)
      .JoinQueryOver<Product>(g => g.Products)
      .Where(p => p.ProductID == Product.ProductID)
      .And(() => innerDiscount.FomDato <= DateTime.Now 
              && innerDiscount.TomDato >= DateTime.Now).List()
      .Select( Projections.Property(() => innerDiscount.ID))
 ;

var query = session.QueryOver<Discount>(() => discount)
  .Where(Subqueries.PropertyIn("ID", subQuery.DetachedCriteria))
  ;