Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI and OData - returning Queryable with preconditions

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 ?

like image 484
Evgeni Avatar asked Oct 19 '25 03:10

Evgeni


1 Answers

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.

like image 128
Matt Johnson-Pint Avatar answered Oct 20 '25 18:10

Matt Johnson-Pint