views:

322

answers:

1

I'm trying order a Linq to NHibernate query by the sum of it's children.

session.Linq<Parent>().OrderBy( p => p.Children.Sum( c => c.SomeNumber ) ).ToList()

This does not seem to work. When looking at NHProf, I can see that it is ordering by Parent.Id. I figured that maybe it was returning the results and ordering them outside of SQL, but if I add a .Skip ( 1 ).Take( 1 ) to the Linq query, it still orders by Parent.Id.

I tried doing this with an in memory List and it works just fine.

Am I doing something wrong, or is this an issue with Linq to NHibernate?

I'm sure I could always return the list, then do the operations on them, but that is not an ideal workaround, because I don't want to return all the records.

+1  A: 

To order a query by an aggregate value, you need to use a group by query. In your example you need to use a 'group by' with a join.

The equivelant SQL would be something like:

select id, sum(child.parentid) as childsum from dbo.Parent
inner join child on
parent.id= child.parentid 
group by id
order by childsum desc

If only Linq2NH could do this for us... but it can't sadly. You can do it in HQL as long as you're ok with getting the id, and the sum back, but not the whole object.

I battled with Linq2NH for months before I abandoned it. Its slow, doesn't support 2nd level cache and only supports VERY basic queries. (at least when I abandoned it 6 months ago - it may have come along leaps and bounds!) Its fine if you are doing a simple home-made application. If your application is even remotely complex ditch it and spend a bit of time getting to know HQL: a little harder to understand but much more powerful.

reach4thelasers
The reason I don't want to use HQL is re-factoring. Re-factoring won't catch the HQL strings. This is the same reason I use FluentNHibernate instead of .hbm files.I do need to get the whole object back though, I just want it ordered by an aggregate. I can do it just fine in SQL, so HQL probably supports it too, if I really need to go that route.I'm not sure about the performance of Linq2NH. It seems fast enough right now. Maybe the 1.0 release has some improvements. It's supposed to pass most of the NHibernate query tests (so I've read, not even sure what they are though).
Josh Close
http://hiberlog.wordpress.com/2009/02/16/lambda-expressions-extension-methods-and-nhibernate/Here's a page I bookmarked earlier today about using Extension methods to stongly type Criteria Queries. I've not read it yet, but you might find it interesting since you are concerned about having strongly typed queries.Sorry I couldn't be of more help!
reach4thelasers
I've actually used NHLambdaExtensions before. http://code.google.com/p/nhlambdaextensions/ This worked pretty well, but I really dislike criteria queries. I'd much rather use HQL and have unit tests check for errors.
Josh Close
So, do you know if this is a bug in Linq to NHibernate, or is it just not supported?
Josh Close
I asked the same thing over at NHUsers about 8 months ago:http://groups.google.com/group/nhusers/browse_thread/thread/1d46f5625a5f75b1/fa8a32e6612322cd?q=#fa8a32e6612322cdThe response from the main Linq2NH developer "Grouping queries are currently not well supported in nh.linq. This is a bug you have run into." as I said... in an ideal world Linq2NH would be great, but its too immature. Only very basic queries supported.Thanks for pointing out NHLambdas - I am going to try it! Good luck with your project!
reach4thelasers
Man this NHLambdas thing is spectacular! I'm re-implementing all my queries to use it! Thanks for the tick- remember to vote up too! :)
reach4thelasers