Remove OrderBy from QueryOver

82 views
Skip to first unread message

Felipe Oriani

unread,
May 22, 2012, 9:09:34 PM5/22/12
to nhu...@googlegroups.com

Hello Guys, I have an extension method for my queries to page it, this is the code:

        public static PagedList<T> ToPageList<T>(this IQueryOver<T, T> query, int size, int index)
        {
            int total = query.Clone().FutureRowCount();
            var list = query.Take(size).tSkip((index - 1)*size).Future<T>();
            
            return new PagedList<T>(list, total, size, index);
        }

It works fine, but when I use an order by method (on my queryover), the Nhibernate process this order by command on the count query. 
My question is, is there any way to remove the order by from the QueryOver to NHibernate count it without order by?

Or if anyone has any suggestions to improve this method, I would appreciate.

PS: I use it with asp.net mvc

Thank you!


--
______________________________________
Felipe B. Oriani
Contato: (19) 9611-8646 / (19) 3421-7850

"...Trabalhe quanto puder, tornando-se útil quanto possível..." , por André Luiz


Oskar Berggren

unread,
May 23, 2012, 1:52:51 AM5/23/12
to nhu...@googlegroups.com
Are you sure it matters? Wouldn't the database ignore the order by as
an optimization?

/Oskar


2012/5/23 Felipe Oriani <felipe...@gmail.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.

pvginkel

unread,
May 23, 2012, 5:16:58 AM5/23/12
to nhu...@googlegroups.com
You could provide the order by as an extra (optional) parameter. The signature for order by can be taken from  http://msdn.microsoft.com/en-us/library/bb341409  and this way you can apply it only on the list query. Expression (probably) has methods to combine the two expressions.


On Wednesday, May 23, 2012 3:09:34 AM UTC+2, Felipe Oriani wrote:

Hello Guys, I have an extension method for my queries to page it, this is the code:

        public static PagedList<T> ToPageList<T>(this IQueryOver<T, T> query, int size, int index)
        {
            int total = query.Clone().FutureRowCount();
            var list = query.Take(size).tSkip((index - 1)*size).Future<T>();
            
            return new PagedList<T>(list, total, size, index);
        }

It works fine, but when I use an order by method (on my queryover), the Nhibernate process this order by command on the count query. 
My question is, is there any way to remove the order by from the QueryOver to NHibernate count it without order by?

Or if anyone has any suggestions to improve this method, I would appreciate.

PS: I use it with asp.net mvc

Thank you!


--
______________________________________
Felipe B. Oriani

Felipe Oriani

unread,
May 23, 2012, 10:13:27 AM5/23/12
to nhu...@googlegroups.com
Yes, I don't want the nhibernate ordernig my count query. My code is something like this:


public PagedList<Product> GetProducts() {
   
   return Session.QueryOver<Product>().OrderBy(x => x.Price).Desc.ToPagedList<Product>();

}

with this extension method:

public static PagedList<T> ToPageList<T>(this IQueryOver<T, T> query, int size, int index)
        {
            int total = query.Clone().FutureRowCount();
            var list = query.Take(size).tSkip((index - 1)*size).Future<T>();
            
            return new PagedList<T>(list, total, size, index);
        }

And NHibernate generates two query with future like:

select count(*) from Product order by price desc // want to remove this order by...
select ... from product order by price desc

is there any way?




--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/-VZsmX85IosJ.

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.



--
______________________________________
Felipe B. Oriani

Dan B

unread,
May 23, 2012, 11:49:41 AM5/23/12
to nhu...@googlegroups.com
I always Clone the criteria to get the RowCount before applying the Ordering.
-Dan
To unsubscribe from this group, send email to nhusers+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.



--
______________________________________
Felipe B. Oriani

Felipe Oriani

unread,
May 23, 2012, 2:53:06 PM5/23/12
to nhu...@googlegroups.com
Yes it would work, but I have an extension method that clone the query and apply RowCount but it's keeping OrderBy. I think I'll try something like Dan said!

But would be nice if NHibernate remove orderby clausules from count queries. :)

Thanks guys


To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/rCA4iRK1_UwJ.

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.



--
______________________________________
Felipe B. Oriani

Darren Kopp

unread,
May 23, 2012, 9:20:16 PM5/23/12
to nhu...@googlegroups.com
what does the actual query look like? I bet if you look at the execution plan that the query compiler is ignoring the order by so it probably doesn't matter if you have it or not.


On Tuesday, May 22, 2012 7:09:34 PM UTC-6, Felipe Oriani wrote:

Hello Guys, I have an extension method for my queries to page it, this is the code:

        public static PagedList<T> ToPageList<T>(this IQueryOver<T, T> query, int size, int index)
        {
            int total = query.Clone().FutureRowCount();
            var list = query.Take(size).tSkip((index - 1)*size).Future<T>();
            
            return new PagedList<T>(list, total, size, index);
        }

It works fine, but when I use an order by method (on my queryover), the Nhibernate process this order by command on the count query. 
My question is, is there any way to remove the order by from the QueryOver to NHibernate count it without order by?

Or if anyone has any suggestions to improve this method, I would appreciate.

PS: I use it with asp.net mvc

Thank you!


--
______________________________________
Felipe B. Oriani
Reply all
Reply to author
Forward
0 new messages