Reputation: 220
I've been searching around and cannot seem to find an answer to this.
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 lies in, that this query doesn't work because of someOverridingFlag, "cannot query on fields that are not indexed".
I am aware that implicitly raven's creating an index in the background. But how exactly do you read external variables and include it as part of the query expression as well?
The only work around I have right now is to have different varied queries and to check the flag first then query differently.
Am I doing anything incorrectly?
Thanks in advance!
Upvotes: 3
Views: 215
Reputation: 13224
If - as is the case in your example - the "external" criteria do not translate to an expression that involves one of the query's accessible fields, then - besides the fact it is not possible - it also makes no sense to include them in the query.
For the rather contrived example you provided, you could 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 a really bad API design, because if the someOverridingFlag
is null
, the method does not do what its name promises. It would be better to include explicit methods for the two different cases.
public List<EntityA> GetAllItemsGreaterThanTwenty();
public List<EntityA> GetAllItems();
Another important issue, is that you are performing non-paged queries over a potentially unbounded results set. RavenDB will limit the number of results, i.e. if the number of possible matches exceeds 128 or 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 those topics.
Upvotes: 2