Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7854927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:14:16+00:00 2026-06-02T20:14:16+00:00

I have simple set of objects stored in RavenDB: public class Question { public

  • 0

I have simple set of objects stored in RavenDB:

public class Question
{
    public string Id { get; set; }
    public DateTime CreatedOn { get; set; }
    public ICollection<User> Supporters { get; set; }
    public ICollection<Answer> Answers { get; set; }
}

public class Answer 
{
    public string Id { get; set; }
    public bool IsOfficial { get; set; }
}

Now I want to query RavenDB to give me set of questions, ordered firstly by number of supporters, next by condition – if a question has any official answer, and in the and, by question creation date. So I’ve written a query:

var questions = DocumentSession.Query<Question>().AsQueryable(); 
questions = questions
                .OrderByDescending(x => x.Supporters.Count)
                .ThenByDescending(x => x.Answers.Any(a => a.IsOfficial)) //EDIT: source of exception
                .ThenByDescending(x => x.CreatedOn)
                .Take(15);                
var result = questions.ToList();

which throws an exception:

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

The query is logically correct and works, when I use linq-to-objects, and simply add .ToList() to first line:

var questions = DocumentSession.Query<Question>().Tolist().AsQueryable();
// next lines stay unchanged

I don’t want to do it because of performance issues (this change forces all questions to be loaded from database into memory before filtering).

How to make this working without performance impact ? Maybe shell I define an index ? How it should looks like then ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:14:17+00:00Added an answer on June 2, 2026 at 8:14 pm

    Based on Bear Alexander response, I’ve done this like that:

    public class QuestionByAnyOfficial : AbstractIndexCreationTask<Question, QuestionByAnyOfficial.Result>
    {
        public class Result
        {
            public string Id;
            public bool AnyOfficial;
            public int SupportersCount;
            public DateTime CreatedOn;
        }
    
        public QuestionByAnyOfficial()
        {
            Map = questions => from question in questions                               
                               select new
                                  {
                                      Id = question.Id,                                              
                                      AnyOfficial = question.Answers.Any(a => a.IsOfficial),
                                      SupportersCount = question.Supporters.Count,
                                      CreatedOn = question.CreatedOn
                                  };            
        }
    }
    
    var questionIds = DocumentSession.Query<QuestionByAnyOfficial.Result, QuestionByAnyOfficial>()
                           .OrderByDescending(x => x.SupportersCount)
                           .ThenByDescending(x => x.AnyOfficial)
                           .ThenByDescending(x => x.CreatedOn)
                           .Take(NumberOfQuestions)
                           .Select(x => x.Id);
    var questions = DocumentSession.Load<Question>(questionIds);
    var result = questions.ToList();
    

    It works and I believe it is more efficient than my original version. If it can be done in any more elegant way, I’d appreciate any ideas. Regards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class as defined below: public class Person { int _id;
I have a set of objects stored in a variable called subs . The
Say I have a simple class like this: public class ReferenceChanger<T> { public T
What I am trying to do: I have a simple set union function in
I have a fairly simple set of functionality for which I have multiple implementations,
I have a very simple .htaccess file set up to redirect a 404 to
I have a simple SQL table which defines a set of hierarchical categories and
I have a simple on my page with the opacity being set to 0.5.
Right so I have a simple app that consists of a calendar a Set
I have a small (500kb) swing applet that displays very simple/limited set of small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.