gpt4 book ai didi

NHibernate QueryOver 与 JoinQueryOver 选择所有列

转载 作者:行者123 更新时间:2023-12-04 05:13:07 26 4
gpt4 key购买 nike

我有一些带有多个连接的 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 对象的列表。我可以使用投影获得 Discount 的一些属性,并且查询会小得多。但是我如何指示 NHibernate 只从 Discount 表中获取列而不是从所有连接表中获取列?

最佳答案

据我所知,没有像这样简单的方法:.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))
;

关于NHibernate QueryOver 与 JoinQueryOver 选择所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14648110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com