Dynamic OrderBy

478 views
Skip to first unread message

Johan Nordberg

unread,
Oct 14, 2011, 4:57:27 AM10/14/11
to rav...@googlegroups.com
I have a query where I don't know the sort order at compile time. I'd like to pass the sorting as a parameter to the method.

var sort = <please help me create a dynamic sort expression>;
var query = DocumentSession.Query<Post>()
                .OrderByDescending(sort)
                .Skip(start)
                .Take(count)
                .ToList();

How can I init the sort variable and .OrderByDescending to set the sort order at runtime?


Ryan Heath

unread,
Oct 14, 2011, 5:06:04 AM10/14/11
to rav...@googlegroups.com
var query = DocumentSession.Query<Post>()
.Skip(start)
.Take(count);

switch(...)
{
case ...: query = query.OrderByDescending(...); break;
case ...: query = query.OrderByDescending(...); break;
case ...: query = query.OrderByDescending(...); break;
...
}

var list = query.ToList();

Does this helps?

// Ryan

Johan Nordberg

unread,
Oct 14, 2011, 5:30:13 AM10/14/11
to rav...@googlegroups.com
Is is not possible to create linq expression dynamicly? 


Ryan Heath

unread,
Oct 14, 2011, 5:47:18 AM10/14/11
to rav...@googlegroups.com
What do you mean?

The sample code should get you started ...
based on your method input you can attach a different orderby.

// Ryan

Johan Nordberg

unread,
Oct 14, 2011, 7:55:11 AM10/14/11
to rav...@googlegroups.com
Yes, but let's say my model has 20 properties that I could sort on, both asc and desc. That would be pretty big switch statement... 

What I was thinking that maybe there was a more dynamic way to say x => x.ProperyName, like ParseExpression<MyType>(string propertyName) and that would be something you could pass into OrderByAscending or OrderByDescending.

// Johan

Mauro Servienti

unread,
Oct 14, 2011, 7:56:58 AM10/14/11
to rav...@googlegroups.com
you can start from here:

and built your one predicate builder for OrderBy

.m

Ayende Rahien

unread,
Oct 14, 2011, 10:35:28 AM10/14/11
to rav...@googlegroups.com
You can use the untyped DocumentSession.Query<...>().OrderBy(string) method

Chris Marisic

unread,
Oct 14, 2011, 11:23:41 AM10/14/11
to rav...@googlegroups.com
I'm going to need to remember this, I just recently did some shitty switch case code to do orderbys instead of trying to build predicates dynamically.

Johan Nordberg

unread,
Oct 15, 2011, 2:36:28 PM10/15/11
to rav...@googlegroups.com
Thanks exactly what I was looking for, but I cant find any overload for OrderBy that accepts a string. Am I missing some using statement? In which namespace is this defined?



Ayende Rahien

unread,
Oct 15, 2011, 3:22:25 PM10/15/11
to rav...@googlegroups.com
It is not on IQueryable, it is on LuceneQuery

Johan Nordberg

unread,
Oct 16, 2011, 4:58:27 PM10/16/11
to rav...@googlegroups.com
Thanks, sorting works great, but my where filters dont work. Every document is returned. What am I doing wrong?
Properties is defined as Dictionary<string, string>.

var query = DocumentSession.Advanced.LuceneQuery<Post>()
                .Include(x => x.UserId)
                .Statistics(out stats)
                .WhereEquals("IsOnline"true)
                .WhereEquals("Country", country)
                .WhereEquals("Properties." + propertyName, propertyValue)
                .OrderBy(orderBy)
                .Skip(start)
                .Take(count)
                .ToList();


Ayende Rahien

unread,
Oct 17, 2011, 1:14:32 AM10/17/11
to rav...@googlegroups.com
What is sent to the server?

Johan Nordberg

unread,
Oct 17, 2011, 3:28:40 AM10/17/11
to rav...@googlegroups.com
Request # 150: GET     -    58 ms - <default>  - 200 - /indexes/dynamic/Posts?query=IsOnline%253Atrue%2520Country%253Asv%255C-se%2520Properties.Color%253Ablack&
start=0&pageSize=10&aggregation=None&sort=-Created&include=UserId

Thanks for helping me!

Ayende Rahien

unread,
Oct 17, 2011, 4:39:27 AM10/17/11
to rav...@googlegroups.com
Well,
This is the actual query being sent:

IsOnline:true Country:sv\-se Properties.Color:black

And it seems to be a good one. Can you try creating a failing test?

Itamar Syn-Hershko

unread,
Oct 17, 2011, 7:17:51 AM10/17/11
to rav...@googlegroups.com
I _think_ we should be escaping sv\-se

Can you try sending Country:sv\\-se?

Itamar Syn-Hershko

unread,
Oct 17, 2011, 7:18:26 AM10/17/11
to rav...@googlegroups.com
Or I got this completely wrong and country==sv-se

Ayende Rahien

unread,
Oct 17, 2011, 7:18:44 AM10/17/11
to rav...@googlegroups.com
I think that \- _is_ the escape, it is sv-se, proably

On Mon, Oct 17, 2011 at 1:17 PM, Itamar Syn-Hershko <ita...@hibernatingrhinos.com> wrote:

Johan Nordberg

unread,
Oct 17, 2011, 8:14:26 AM10/17/11
to rav...@googlegroups.com
It should be country=sv-se, so the escaping is done by raven I guess?!

Ayende Rahien

unread,
Oct 17, 2011, 8:22:52 AM10/17/11
to rav...@googlegroups.com
Yes, that is correct and should be done

Matt Warren

unread,
Oct 17, 2011, 9:09:58 AM10/17/11
to ravendb
The query
IsOnline:true Country:sv\-se Properties.Color:black

is the same as the query below (because OR is the default if no
operator is supplied)
IsOnline:true OR Country:sv\-se OR Properties.Color:black

With your data does this query match all the records?

On Oct 17, 1:22 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Yes, that is correct and should be done
>

Johan Nordberg

unread,
Oct 17, 2011, 9:49:03 AM10/17/11
to rav...@googlegroups.com
Yes, everything has IsOnline == true right now.

How should this be written to AND all filters?

Ayende Rahien

unread,
Oct 17, 2011, 9:51:12 AM10/17/11
to rav...@googlegroups.com
Where().AndAlso().Where()

Johan Nordberg

unread,
Oct 17, 2011, 10:47:29 AM10/17/11
to rav...@googlegroups.com
Thanks everyone! Got it working!


Reply all
Reply to author
Forward
0 new messages