I'm using the current Linq provider for NHibernate (version 2.1).
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()}
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.
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()}
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()};
来源:https://stackoverflow.com/questions/1983477/linq-to-nhibernate-and-group-by