3

I am using RavenDB and running into problems when trying to order my results.

Here's a sample of the classes.

public class Post {
    public int Id {get;get;}
    public DateTimeOffset Posted {get;set;}
    public List<SectionAssignment> Sections{get;set;}
    public string Headline {get;set;}
}
public class SectionAssignment {
    public int SectionId {get;set;}
    public int Priority {get;set;}
}

In my controller I am trying to return a set of Posts which is assigned to a specific Section, then order by (desc) the date portion of the Posted property. Then I want to to sort by the SectionAssignment Priority. Here's what I've got:

var posts= RavenSession.Query<Post>()
    .Where(s => s.Sections.Any(sec=>sec.SectionId==5))
    .OrderByDescending(s => s.Posted.Date)
    .ThenBy(s => s.Section.Where(sec => sec.Id == 5).Select(sc => sc.Priority).Single())
    .Take(10)
    .ToList();

I am the following exception:

Unable to cast object of type 'System.Linq.Expressions.MethodCallExpressionN' to type 'System.Linq.Expressions.MemberExpression'.

I can see why this is happening, but I can't figure out how to make this work. Any suggestions?

1 Answer 1

6

An educated guess is that it's your ThenBy clause that's causing it to fail.

If I remember correctly, Ordering and Querying predicates can't refer to other fields - it's a limitation of Lucene.

A work-around is to instead set up a manual index which pulls out the sorting priority as a field, which you can then order by.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.