c# - 如何在 NHibernate 中选择前 10 名最畅销的产品?

标签 c# nhibernate fluent-nhibernate nhibernate-criteria

此解决方案对我不起作用: C#/NHibernate - Get first 10 records ordered by grouped sum

我的情况非常相似,但我一直收到以下错误消息:

Additional information: could not resolve property: Produto_Id of: SistemaVendas.Domain.venda.ProdutoVendido

到目前为止我的代码:

ICriteria criteria = unitOfWork.Session
            .CreateCriteria<ProdutoVendido>("ProdutoVendido")
            .SetMaxResults(limite)
            .CreateCriteria("Produto_Id")
                .SetProjection(Projections.ProjectionList()
                    .Add(Projections.GroupProperty("Produto_Id"), "ID")
                    .Add(Projections.Sum("ProdutoVendido.Quantidade"), "QuantitySum")
                )
                .AddOrder(Order.Desc("QuantitySum"));

return criteria
            .SetResultTransformer(Transformers.AliasToBean<Produto>())
            .List<Produto>().ToList();

我做错了什么?

附言ProdutoVendido = SoldProduct 和 Produto = Product

ProdutoVendido 映射

    public ProdutoVendidoMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();            

        Map(x => x.Quantidade);
        Map(x => x.Desconto);
        Map(x => x.Observacao).Length(ProdutoVendido.MAX_OBSERVACAO_LENGTH);            

        References(x => x.Venda)
            .Cascade.None();
        References(x => x.Produto)
            .Cascade.None();
    }

编辑:

最终解决方案

正如@Radim Köhler 指出的那样,我刚刚创建了这个 DTO

public class ProdutoDto
{
    public ulong Id { get; set; }
    public int QuantidadeTotal { get; set; }
}

我最后的 NHibernate 查询是

ICriteria criteria = unitOfWork.Session
    .CreateCriteria<ProdutoVendido>("ProdutoVendido")
    .SetMaxResults(limite)            
    .CreateCriteria("Produto", "p")            
        .SetProjection(Projections.ProjectionList()
            .Add(Projections.GroupProperty("p.Id"), "Id")
            .Add(Projections.Sum("ProdutoVendido.Quantidade"), "QuantidadeTotal")                    
        )
        .AddOrder(Order.Desc("QuantidadeTotal"));

var ids = criteria
            .SetResultTransformer(Transformers.AliasToBean<ProdutoDto>())
            .List<ProdutoDto>();

return unitOfWork.Session
    .CreateCriteria<Produto>()                
    .Add(Restrictions.In("Id", ids.Select(x => x.Id).ToArray()))
    .List<Produto>().ToList();

最佳答案

要修复 NHibernate 返回的错误,我们必须查询现有的对象模型。在这种情况下,售出的产品 (ProdutoVendido) 引用 Produto,而不是 Produto_Id

...
Session
  .CreateCriteria<ProdutoVendido>("ProdutoVendido")
  ...
  // this line is about joining reference
  // not the column
  //.CreateCriteria("Produto_Id")
  .CreateCriteria("Produto", "p") // "p" is now alias which we can use

检查类似的工作查询,例如here

延长

我们需要能够投影 SUM 属性的是 DTO (不是 POCO, just DTO 用于此目的)。查看此问答以获取灵感 Joining two tables with specific columns in nhibernate

可能是这样:

public class ProdutoDto
{
    public virtual int ID { get; set; }
    public virtual decimal QuantitySum { get; set; } // the type should match
}

我们可以将其用作投影目标:

return criteria
        .SetResultTransformer(Transformers.AliasToBean<ProdutoDto>())
        .List<ProdutoDto>().ToList();

关于c# - 如何在 NHibernate 中选择前 10 名最畅销的产品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37123788/

相关文章:

c# - 请帮助解决此错误 : Operator '^' cannot be applied to operands of type 'double' and 'int'

c# - 将 StartTLS 与 System.DirectoryServices 中的 LDAP 结合使用

.net - Session-in-view 和 TransactionScope

nhibernate - Fluent Nhibernate 在映射中放置了一个 where 子句

c# - 我可以阻止客户端下载在浏览器上可见的 pdf 文件吗

c# - 将字符串数组从 VB6 传递到 C#.net

nhibernate - Entity Framework 4 的存储库模式

nHibernate:使用 QueryOver 的连接和分离

nhibernate - 从 Fluent Nhibernate 生成 XML 映射

c# - 在 nhibernate 中,无法更新子对象列表