NHibernate Linq Group By 无法在 SQL Server 中正确分组 [英] NHibernate Linq Group By fails to group properly in SQL Server

查看:27
本文介绍了NHibernate Linq Group By 无法在 SQL Server 中正确分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 LINQ 查询,它对 SQL Server 支持的存储库使用 NHibernate...

I have the following LINQ query which uses NHibernate against a SQL Server backed repository...

var casesByCaseOwner = this.preGrantDetailRepository.All
   .Where(x => x.CaseFileLocation.Id == cflId)
   .GroupBy(x => x.CaseOwner)
   .Select(x => new StagSummaryForCfItem
   {
      Id = x.Key.Id,
      Description = x.Key.Name,
      NumberOfCases = x.Count(),
      UninvoicedNetFee = x.Sum(y => y.UninvoicedNetFee),
      UninvoicedDisbursement = x.Sum(y => y.UninvoicedDisbursement)
   }).AsEnumerable();

但是,它抱怨 SQL Server 无法按 CaseOwner.Name 列分组,因为它没有包含在选择列表或组子句中.来自数据库世界,我理解这个错误,但是,我不确定如何强制 NHibernate 按 IdName 分组,但仍然有 CaseOwner 在我的选择中可用的实体.

However, it complains that SQL Server is unable to group by the CaseOwner.Name column because it is not contained in the select list or group clause. Coming from a database world I understand that error, however, I'm not sure how to force NHibernate to group by both Id and Name but still have the CaseOwner entity available to me in my Select.

推荐答案

我终于找到了答案...

I found the answer finally...

     var casesByCaseOwner = this.preGrantDetailRepository.All
     .Where(x => x.CaseFileLocation.Id == cflId)
     .GroupBy(x => new { x.CaseOwner.Id, x.CaseOwner.Name })
     .Select(x => new StagSummaryForCfItem
     {
        Id = x.Key.Id,
        Description = x.Key.Name,
        NumberOfCases = x.Count(),
        UninvoicedNetFee = x.Sum(y => y.UninvoicedNetFee),
        UninvoicedDisbursement = x.Sum(y => y.UninvoicedDisbursement)
     }).AsEnumerable();

     return casesByCaseOwner;

这很有效,事实证明我需要用我想要分组的属性来投影一个新实体.

This works nicley, it turns out I need to project a new entity with the properties I want to group on.

这篇关于NHibernate Linq Group By 无法在 SQL Server 中正确分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆