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

查看:83
本文介绍了NHibernate Linq分组依据无法在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进行分组,但在我的Select中仍然可以使用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分组依据无法在SQL Server中正确分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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