开发者

HNibernate: help to avoid N+1 query

开发者 https://www.devze.com 2023-01-16 04:18 出处:网络
I\'ve got following query DetachedCriteria criteria = DetachedCriteria.For(typeof(Income)) .CreateAlias(\"Product\", \"p\")

I've got following query

DetachedCriteria criteria = DetachedCriteria.For(typeof(Income))
                .CreateAlias("Product", "p")
                .SetProjection(
                    Projections.ProjectionList()
                        .Add(Projections.GroupProperty("Product")开发者_JS百科)
                        .Add(Projections.Sum("Quantity"))
                 );

which is translated to sql:

SELECT   this_.Product_id    as y0_,
         sum(this_.Quantity) as y1_
FROM     income this_
         inner join products p1_
           on this_.Product_id = p1_.Id
GROUP BY this_.Product_id

my domain:

class Product
{
    IList<Income> Incomes {get;set;}
}

class Income
{
    Product Product {get;set;}
}

when iterating returned collection I have one query for each Product entity. How can I grab all collection in one query?


I prefer HQL for that kind of query...

select i.Product, sum(i.Quantity)
from Income i
group by /*all product properties*/


Did you use Enumerable instead of List to execute the query?

0

精彩评论

暂无评论...
验证码 换一张
取 消