简体   繁体   English

nHibernate Projections.Sum 连接列

[英]nHibernate Projections.Sum for joined columns

I am creating a query with Criteria like this:我正在使用这样的条件创建一个查询:

DetachedCriteria auftragCriteria = DetachedCriteria.For<Auftrag>("a");

I join multiple tables with:我加入多个表:

DetachedCriteria positionJoin = auftragCriteria.CreateCriteria("a.Positionen", "p", JoinType.LeftOuterJoin);

And I use a projection to fill in my object SubTypeAuftrag我使用投影来填写我的 object SubTypeAuftrag

ProjectionList projectionListSubTypeAuftrag = Projections.ProjectionList();

Now I need to recreate the following sql code:现在我需要重新创建以下 sql 代码:

cast(sum(p.length * p.width / 1000) as decimal)

I tried the following:我尝试了以下方法:

projectionListSubTypeAuftrag.Add(Projections.Sum<Position>(p => p.length * p.width / 1000), "M1");

This leads to an error:这会导致错误:

System.InvalidOperationException: 'variable 'p' of type 'xxx.Base.Position' referenced from scope '', but it is not defined'

I also tried:我也试过:

        projectionListSubTypeAuftrag.Add(
            Projections.Cast(
                NHibernateUtil.Decimal,
                Projections.SqlProjection("p.length * p.width/ 1000 AS result", new[] { "result" }, new IType[] { NHibernateUtil.Double })
                ),
            "M1"
            );

How can I tell nHibernate where to find the length/width column?如何告诉 nHibernate 在哪里可以找到长度/宽度列?

Maybee this will point you in the right direction.也许这会为您指明正确的方向。

  var sqlMultiply = new VarArgsSQLFunction("(", "*", ")");
            var sqlDivide = new VarArgsSQLFunction("(", "/", ")");
            var multiplyLengthWidthProj = Projections.SqlFunction(sqlMultiply, NHibernateUtil.Decimal, Projections.Property(() => alias.Length), Projections.Property(() => alias.Width));

            var sumProjection = Projections.ProjectionList().Add(Projections.Sum(Projections.SqlFunction(sqlDivide, NHibernateUtil.Decimal, multiplyLengthWidthProj, Projections.Constant(1000))));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM