c#Query Interceptors and complex lambda expressions

So we have a WCF Data Service exposing OData with an Entity Framework (v5.6) model.
Since we wanted to filter the data based on authentication and access control, some Query Interceptors were added. At first, all they had to do was get the user's scope and match that to an entity's property - something like this:

public Expression<Func<Entity, bool>> EntitiesInterceptor()
    // Get user scope
    string userScope = GetUserScope();
    return entity => entity.Scope.StartsWith(userScope));

This worked as expected, and everything was well with the world.
Later on, we wanted to evolve the interceptor to allow matching with a list of scopes. So we changed the code to something like this:

// Get user scopes
string[] validScopes = GetUserScopes();
return entity => validUnits.Any(scope => entity.Scope.StartsWith(scope));

This seemed to work as long as our OData requests didn't expand beyond one level of relations. So, for instance, stuff like:

  • /ODataService.svc/Entity?$expand=OtherEntity
  • /ODataService.svc/Entity?$expand=OtherEntity,AnotherEntity

...would process just fine. However, when we start querying deeper relations:

  • /ODataService.svc/Entity?$expand=OtherEntity/YetAnotherEntity

We start getting an 'Object reference not set to an instance of an Object' error from System.Entity.Data. The stack trace is really long and mentions lots of classes on the System.Data namespace. Sadly, it does not mention any line in our code.

Link to full stack trace

It seems to us that the problem is on how EF is converting our lambda expressions to queries. We've tried replacing the .Any() with other equivalents like .Count(expression) > 0 in hopes that the conversion would work, but alas, the result was the same.

So, can anyone think of a different way to approach the problem? Has somebody faced the same issue? Or is it something that we're doing wrong?

Thanks in advance for reading the huge wall of text above.

Related Articles
  • I need to build a complex lambda expression in runtime, here is the cenario: I have a interface named IHasCostCenter with a string property named CostCenterId: public interface IHasCostCenter { string CostCenterId { get; set; } } Here is a example of
  • Does the standard define what happens with this code? #include <iostream> template <typename Func> void callfunc(Func f) { ::std::cout << "In callfunc.\n"; f(); } template <typename Func> void callfuncref(Func &f) { :
  • I'm trying to switch this linq code to a lambda expression: foreach (var id in orderLinesToSplit) { int result = (from ol in orderLines from splittedOl in orderLines where ol.Key == id && ol.Value == splittedOl.Value && ol.Key != splittedO
  • A simple question regarding lambda expression I wanted to get an average of all trades in following code. The formula I am using is ((price 1*qty 1+(price 2*qty 2)....+(price n*qty n)/(qty 1+qty 2+...+qty n) In following code, I am using sum function
  • This is my join in linq I just think is there any possible way to write it with lambda: Does any one have any idea about this? any suggestion? All LINQ query expressions can be converted into "dot notation" (method calls) - that's basically what
  • I was looking through Java source code for the Map interface and ran into this little snippet of code: /** * Returns a comparator that compares {@link Map.Entry} in natural order on value. * * <p>The returned comparator is serializable and throws {@
  • In VB.NET, a lambda expression can be declared and invoked on the same line: 'Output 3 Console.WriteLine((Function(num As Integer) num + 1)(2)) Is this possible in C#? You have to tell the compiler a specific delegate type. For example, you could cas
  • I'm trying to learn how to use lambda expressions in C++. I tried this simple bit of code but I get compile errors: int main() { vector<int> vec; for(int i = 1; i<10; i++) { vec.push_back(i); } for_each(vec.begin(),vec.end(),[](int n){cout <&l
  • I have a misunderstanding while converting simple logic formula to lambda expression (proof of that formula). So, I have the following formula: ((((A->B)->A)->A)->B)->B where -> means implication logical operator. How can I write some la
You Might Also Like