Is LINQ group by
still not supported in NH 3.0 GA or I'm doing something wrong ?
My LINQ is :
var q = from p in session.Query<Product>()
group p by p.Category into g
select new {
Category = g.Key,
TotalValue = g.Sum(p => p.UnitsInStock * p.Price)
};
Witch is conver开发者_JS百科ted to the following SQL :
select category1_.Id as col_0_0_,
cast(sum(product0_.UnitsInStock*product0_.Price) as DOUBLE PRECISION) as col_1_0_,
category1_.Id as Id0_,
category1_.Name as Name0_
from [Product] product0_
left outer join [Category] category1_ on product0_.Category_id=category1_.Id
group by category1_.Id
Because category1_.Name isn't in the group by clause this generates an SqlException.
Is this a known bug ? Is there a workaround ? This LINQ works well in EF 4.
NHibernate's group by
is not smart enough to add all the properties of an entity when grouping by it.
An alternative would be to use the Id only in the select list and then use session.Load
in Linq-to-objects to project the Category entity.
精彩评论