Custom ordering in an NHibernate Linq query?

315 views
Skip to first unread message

OwenD

unread,
Apr 2, 2010, 11:47:13 AM4/2/10
to nhusers
Apologies for any errors in this question - I'm somewhat new to
NHibernate, LINQ and C#.

The essential situation is that I have a table "blah" that is mapped
to a Blah domain object by NHibernate (I've just made up the table
name etc. here). I want to perform a paginated search on the "blah"
table, and provide a custom ordering on one of its fields - i.e. the
SQL that I eventually want to be executed is something like:

select top 10 * from blah
where <search_criteria>
order by
case orderCol
when 0 then 1
when 1 then 0
when 2 then 3
when 3 then 2
else 4
end

(in this example the ordering is something completely artificial).

The C# code I'm executing is something like:

ISession.Linq<Blah>().Where(<search_criteria>).OrderBy(x =>
x.OrderCol, new CustomComparer())

where the CustomComparer() class implements my custom ordering
behaviour. However, when I monitor the SQL that NHibernate generates,
I just see something like:

select top 10 * from blah
where <search_criteria>
order by orderCol asc

(i.e my Comparer appears to be ignored - perhaps not too surprising).
My question is whether a custom ordering like this is even possible
using NHibernate?

Diego Mijelshon

unread,
Apr 2, 2010, 2:20:34 PM4/2/10
to nhusers
Create a property mapping on that case expression and use it for sorting.
Example: <property name="WeirdSortingProperty" formula="case orderCol ..." />

   Diego



--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


Fabio Maulo

unread,
Apr 2, 2010, 3:09:32 PM4/2/10
to nhu...@googlegroups.com
Resolveit in client-side breaking the Linq sentence.
perhaps in some very far feature the DataProvider may understand and parse a "CustomComparer" and some other stuff.
Waiting it is better if you don't forget that somebody will transform you Linq sentence in a SQL and there are things not representable in SQL.


2010/4/2 OwenD <owen...@googlemail.com>
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

OwenD

unread,
Apr 5, 2010, 7:21:01 AM4/5/10
to nhusers
Many thanks for the responses. In this case, using the "formula"
attribute gave me the behaviour I wanted.

Owen

On Apr 2, 7:20 pm, Diego Mijelshon <di...@mijelshon.com.ar> wrote:
> Create a property mapping on that case expression and use it for sorting.
> Example: <property name="WeirdSortingProperty" formula="case orderCol ..."
> />
>
>    Diego
>

> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages