2

我有一组简单的对象存储在 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; }
}

现在我想查询 RavenDB 给我一组问题,首先按支持者数量排序,然后按条件排序 - 如果问题有任何官方答案,然后按问题创建日期排序。所以我写了一个查询:

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();

抛出异常:

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

当我使用 linq-to-objects 并简单地将.ToList()添加到第一行时,查询在逻辑上是正确的并且有效:

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

由于性能问题,我不想这样做(此更改强制在过滤之前将所有问题从数据库加载到内存中)。

如何在不影响性能的情况下使其工作?也许 shell 我定义了一个索引?那么它应该是什么样子呢?

4

3 回答 3

3

用于您目的的自定义索引基本上将是您的类的重新创建,其中包含额外的字段(以及一些支持它的逻辑)。似乎您不想在当前类中添加更多字段,您可以在项目中添加更多类吗?

这是一个例子:

public class Question_WithAnyOfficial: AbstractIndexCreationTask<Question>
{
    public class Question_WithAnyOfficial()
    {
        Map = questions => from question in questions
                           // New Anonymous Type
                           select new
                           {
                                Id = question.Id,
                                CreatedOn = question.CreatedOn,
                                Supporters = question.Supporters,
                                Answers = question.Answers,
                                AnyOfficial = question.Answers.Where(a => a.IsOfficial).Any()
                           };
    }
}

然后你可以查询这个:

var questions = DocumentSession.Query<Question_WithAnyOfficial>()
                .OrderByDescending(x => x.Supporters.Count)
                .ThenByDescending(x => x.AnyOfficial)
                .ThenByDescending(x => x.CreatedOn)
                .Take(15)
                .ToList();         

不要忘记,您必须在应用启动时注册索引。

于 2012-04-17T19:52:47.480 回答
2

Raven 不能支持 LINQ 查询中的计算,所以这应该可以工作(删除了问题子句):

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

如果您想包含该逻辑,则需要在您的类中添加一个名为AreAllAnswersOfficial(或类似名称)的字段。然后你可以把它放在子句中:

var questions = DocumentSession.Query<Question>()
                .OrderByDescending(x => x.Supporters.Count)
                .ThenByDescending(x => x.AreAllAnswersOfficial)
                .ThenByDescending(x => x.CreatedOn)
                .Take(15);                
var result = questions.ToList();
于 2012-04-17T17:03:03.090 回答
0

根据 Bear Alexander 的回应,我已经这样做了:

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();

它有效,我相信它比我的原始版本更有效。如果它可以以任何更优雅的方式完成,我会很感激任何想法。问候。

于 2012-04-28T21:32:43.293 回答