2

我有一些带有多个连接的 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 表中获取列,而不是从所有连接表中获取列?

4

1 回答 1

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。子查询

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))
  ;
于 2013-02-01T15:15:24.820 回答