µBio
µBio

Reputation: 10758

Difficulty creating proper RavenDB query

I am trying to implement the logic below in a RavenDB query, but receive

System.NotSupportedException: Could not understand expression

related to the scores.Any expression. I understand why that is, but I'm having a hard time coming up with a working option.

public IRavenQueryable<Person> Apply(IRavenQueryable<Person> query)
{
    var scores = new List<string>();
    if (_a) scores.Add("A");
    if (_b) scores.Add("B");
    if (_c) scores.Add("C");
    if (_u)
    {
        scores.Add("");
        scores.Add(" ");
        scores.Add("\t");
        scores.Add(null);
    }

    return from p in query
           where scores.Any(score => score == p.Score)
           select p;
}

Upvotes: 0

Views: 71

Answers (2)

Wade Wright
Wade Wright

Reputation: 89

If we modified your example to use IDocumentQuery, it should work with:

public IDocumentQuery<Person> Apply(IDocumentQuery<Person> query)
{
    var scores = new List<string>();
    if (_a) scores.Add("A");
    if (_b) scores.Add("B");
    if (_c) scores.Add("C");
    if (_u)
    {
        scores.Add("");
        scores.Add(" ");
        scores.Add("\t");
        scores.Add(null);
    }

    return query.AndAlso().WhereIn(x => x.Score, scores);
}

Your initial document query could look something like:

var myQuery = RavenSession.Advanced.DocumentQuery<Person>();

Upvotes: 0

Wyatt Barnett
Wyatt Barnett

Reputation: 15663

The trick is that the ravendb linq provider isn't operating on your list so scores.Any() makes zero sense to it -- it compiles but as you can see it dies at runtime.

The trick is to reverse field a bit and ask if p.Score is in an array of the scores if I recall correctly.

Upvotes: 1

Related Questions