R – Query interceptor to return true if the entity is in a list of items in ado.net data services

wcf-data-services

I'm using ado.net data services and want to implement row level security in the query interceptor to limit the data to only return data that the user is allowed to see.
The complexity comes in in that the user name for the the user is on another table. So I thought I could retrieve a list of events that the user can see according to the entry in the OnlineSubscription table for that user and then return whether the current event matches any entries that are returned as follows:

[QueryInterceptor("Events")]
public Expression<Func<Events, bool>> QueryEvents()
{

  var allowedEventList = (from os in context.OnlineSubscription
                          from e in os.Events
                         where os.UserName == HttpContext.Current.User.Identity.Name
                         select e;

return e => events.Intersect(new List<Events>
                                         {
                                                 e
                                         }).Any();
}

However this throws a "Not implemented" exception. So my question is: Is there a right way to compare the current entity to a list of entities in the query interceptor?

EDIT: I've also tried:

return e => events.Any(evnt => evnt.Event_Key == e.Event_Key);

without any success (once again getting the "Not implemented" exception).

Best Answer

Apparently this fails because by using the lambda expression events.Any... it's already querying in the initial context (ie. querying on Events straight) and then when you add the events.Any clause it also tries to query on the new context (ie. the nested "from" of Events within OnlineSubscription) so it understandably throws an exception.

I then used:

[QueryInterceptor("Events")]
public Expression<Func<Events, bool>> QueryEvents()
{    
    return e => e.OnlineSubscription.Any(os => os.UserName == HttpContext.Current.User.Identity.Name);
}

Which works since this is just querying in the same context as the initial query and merely traverses through relationships.