LINQ RavenDB paging query using Any, cannot include non-indexed variables in query

I have searched around and cannot find an answer to this question.

Imagine I have the following:

public class EntityA
{
    public string EntityAID { get; set; }
    public List<EntityB> Versions { get; set; }
}

public class EntityB
{
    public int Cost { get; set; }
}


public List<EntityA> GetAllItemsGreaterThanTwenty(bool? someOverridingFlag)
{
   using(var session = GetSession())
   {
       var entityAList = from entA in session.Query<EntityA>()
                         where entA.Versions.Any(entB => someOverridingFlag == null || entB.Cost > 20)
                         select entA
       return entityAList.ToList();
   }
}

      

So the problem is that this query is not working because of someOverridingFlag, "cannot query fields that are not indexed".

I know that raven implicitly creates the index in the background. But how exactly do you read external variables and include them as part of the query expression?

The only work I am currently running into is to have different varied requests and check the flag first and then request in a different way.

Am I doing something wrong?

Thanks in advance!

+3


source to share


1 answer


If - as in your example - "external" criteria does not translate into an expression that includes one of the available query fields, then - besides not being possible - it also makes no sense to include them in the query.

For the rather contrived example you provided, you can simply do this:

public List<EntityA> GetAllItemsGreaterThanTwenty(bool? someOverridingFlag)
{
    using(var session = GetSession())
    {
        if (someOverridingFlag == null)
            return session.Query<EntityA>().ToList(); // Note: unbounded result set will be limited to max 128 docs
        else
            return (from entA in session.Query<EntityA>()       
                    where entA.Versions.Any(entB => entB.Cost > 20)
                    select entA).ToList();
    }
}

      

But that would be very bad API design, because if someOverridingFlag

- null

, the method does not honor its promises name. It would be better to include explicit methods for two different cases.



public List<EntityA> GetAllItemsGreaterThanTwenty();
public List<EntityA> GetAllItems();

      

Another major issue is that you are running unreleased queries over a potentially unlimited set of results . RavenDB limits the number of results, i.e. If the number of possible matches exceeds 128 or the server limit, you will not get all of them.

Refer to http://ravendb.net/docs/article-page/2.0/csharp/intro/safe-by-default and http://ravendb.net/docs/article-page/2.5/csharp/client-api/ advanced / unbounded-results for more information on these topics.

+2


source







All Articles