WebAPI and OData - returning Queryable with preconditions
asp.net-web-api, odata, ravendb
Solution
It should work when the values are copied to a local variable first:
var userName = this.User.Identity.Name;
return session.Query<Message>()
.Where(x => x.TargetUserId == userName ||
x.SourceUserId == userName);
This is because by the time the query is executed, the Raven Client query translator can't resolve the objects expressed in the predicate. By copying them into a local variable, you are passing a constant value into the expression.
I believe this is related to closures. Perhaps someone with more direct knowledge of expression trees can explain better in comments.
Problem
I have a simple GET method, which returns IQueryable, and has some preconditions on query: ``` [Queryable(HandleNullPropagation = HandleNullPropagationOption.False)] public IQueryable<Message> Get() { using (var session = RavenStore.GetSession()) { var messages = session.Query<Message>().Where(x => x.TargetUserId == this.User.Identity.Name || x.SourceUserId == this.User.Identity.Name); return messages; } } ``` This is RavenDB, btw. The issue I'm having is that upon execution the user id is replaced with "[EMPTY_STRING]", so the actual query its running is this: 'TargetUserId:[[EMPTY_STRING]] OR SourceUserId:[[EMPTY_STRING]]' on index ..... which is obviously wrong. If I'm returning List instead of IQueriable - it works fine, so something later in the pipeline changes the query. Does anyone have any insight on how to make this work ?