Linq到NHibernate并分组 [英] Linq to NHibernate and Group By

查看:83
本文介绍了Linq到NHibernate并分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为NHibernate(版本2.1)使用当前的Linq提供程序.

I'm using the current Linq provider for NHibernate (version 2.1).

我有两个实体:VideoGame和GameDeveloper,多对一 他们之间的关系. 我正在尝试执行这种查询,该查询会计算 每个游戏开发者都拥有的视频游戏:

I have two entities: VideoGame and GameDeveloper, with a many-to-one relationship between them. I'm trying to perform a query of this sort, which counts the number of video games each game developer has:

from v in session.Linq<VideoGame>()
group by v.Developer into developerGroup
select new {developerGroup.Key.Name, Count = developerGroup.Count()}

枚举此查询会导致异常-无法解析 Entities.VideoGame的属性键". 现在,如果我按v.Developer.Id分组,则可以使用,但是我无法选择 命名列并在结果中显示.我可以分组 v.Developer.Name,但似乎不正确,因为两个开发人员可能 名称相同.

Enumerating this query causes an exception - "could not resolve property Key of Entities.VideoGame". Now, if I group by v.Developer.Id it works, but I can't select the Name column and show it in the results. I could group by v.Developer.Name, but it doesn't seem right, as two developers might have the same name.

我知道当前的Linq提供程序已不再开发,但是 希望对情况有任何建议.

I know the current Linq provider is not being developed any more, but would appreciate any advice on the situation.

推荐答案

怎么样

How about

from v in session.Linq<VideoGame>()
group by v.Developer into developerGroup
select new {key = developerGroup.Key, count = developerGroup.Count()}

在2.1 NHibernate LINQ提供程序中,好像group by似乎已损坏.不久前,史蒂夫·斯特朗(Steve Strong)博客表示group by位于 trunk 中,因此,如果您觉得冒险并且不愿意等待3.0,那么这可能是一个选择.

Seems like group by is broken in the 2.1 NHibernate LINQ provider. A while ago Steve Strong blogged that group by is in the trunk so if you are feeling adventurous enough and not willing to wait on 3.0 then that could be an option.

或者您可以使用类似这样的蛮力解决方案

Or you could use a brute force solution something like this

from v in (from vg in session.Linq<VideoGame>() select vg).ToList()
group by v.Developer into developerGroup
select new {developerGroup.Key.Name, Count = developerGroup.Count()};

这篇关于Linq到NHibernate并分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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