NHibernate Group按和求和

编程入门 行业动态 更新时间:2024-10-28 06:30:09
本文介绍了NHibernate Group按和求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开始研究NHibernate,但有一个我无法解决的问题,我想知道是否有人可以帮助我.

I am starting a study on NHibernate, and I have a problem I'm not able to solve, I wonder if someone could help me.

映射在正确"地工作,但是当我尝试进行分组和求和时,应用程序返回以下错误:

The mapping is working "correctly" but when I try to do the grouping and the sum, the application returns the following error:

无法解析属性:Course.Price:Persistence.POCO.RequestDetail"

"could not resolve property: Course.Price of: Persistence.POCO.RequestDetail"

var criteria = session.CreateCriteria(typeof(RequestDetail)) .SetProjection( Projections.ProjectionList() .Add(Projections.RowCount(), "RowCount") .Add(Projections.Sum("Course.Price"), "Price") .Add(Projections.GroupProperty("Request"), "RequestId") ) .AddOrder(Order.Asc("RequestId")) .SetResultTransformer(Transformers.AliasToEntityMap) .List();

注意1:当我输入代码.Add(Projections.Sum ("Course.Price"), "Price")时,应用程序将结果正确返回给我.

Note 1: When I take code .Add(Projections.Sum ("Course.Price"), "Price") the application returns me the result up correctly.

注意2:我唯一可以做的方法是运行以下代码:

Note 2: The only way I could do was run the code below:

query.Length = 0; query.AppendLine("select"); query.AppendLine(" s.Id,"); query.AppendLine(" s.Identification,"); query.AppendLine(" sum(c.Price) as Total"); query.AppendLine("from"); query.AppendLine(" Student s"); query.AppendLine("inner join"); query.AppendLine(" Request r on r.StudentId = s.Id"); query.AppendLine("inner join "); query.AppendLine(" Requestdetail rq on rq.RequestId = r.Id"); query.AppendLine("inner join"); query.AppendLine(" Course c on c.Id = rq.CourseId"); query.AppendLine("Group by"); query.AppendLine(" s.Id, s.Identification"); query.AppendLine("Order by"); query.AppendLine("s.Identification"); IQuery criteria = session.CreateSQLQuery(query.ToString()) .SetResultTransformer(Transformers.AliasToBean<Teste>()); IList<Teste> teste = criteria.List<Teste>();

有人遇到过这个问题吗?

Has anyone experienced this problem?

推荐答案

我将为结果映射引入一些DTO

I would introduce some DTO for a result mapping

public class MyDTO { public virtual int RowCount { get; set; } public virtual decimal Price { get; set; } // type depends on SUM result public virtual int RequestId { get; set; } }

然后我们只需要添加JOIN(以避免出现异常消息)

And then we just have to add the JOIN (to avoid the exception message)

var criteria = session.CreateCriteria(typeof(RequestDetail)) // the Course.Price comes from some collection // we have to JOIN it .CreateAlias("Course", "Course")// the first is property name, the second is alias .SetProjection( Projections.ProjectionList() .Add(Projections.RowCount(), "RowCount") .Add(Projections.Sum("Course.Price"), "Price") .Add(Projections.GroupProperty("RequestId"), "RequestId") ) .AddOrder(Order.Asc("RequestId")) .SetResultTransformer(Transformers.AliasToBean<MyDTO>()) ; var list = criteria.List<MyDTO>();

猜测为JOIN,它可以是不同的实体/属性名称,但本质应该清楚.我们需要这样做JOIN.借助DTO,我们可以轻松地将结果转换为已知类型的列表

The JOIN is guessed, it could be different entity/property name, but the essence should be clear. We need to do that JOIN. With a DTO we then easily convert result to a list of known types

更多推荐

NHibernate Group按和求和

本文发布于:2023-11-22 04:20:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NHibernate   Group

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!