Get Linq Expression from FilterQueryOption throw CLR exception

c# entity-framework-6 linq linq-expressions odata


We use the filter string OData standards.

This string needs to be parsed before a whereClause is applied to the EntityFramework6 Model-First produced model.


  1. Transform our DataModelDbContext to IEdmlModel Use the NuGet package for Breeze EdmBuilder.
  2. Develop aODataQueryContext using thisEdmModel .
  3. Develop aFilterQueryOption using theODataQueryContext the filter string, and.

Right now, there is aFilterQueryOption object has an internal expression tree that is well-formed.

When we translate this expression tree to Linq, we run into issues (used in the EF Where clause)

On the internet, we discovered the following conversion technique: (Exception message inside)

static private Expression<Func<Countries, bool>> GetFilterExpression(FilterQueryOption filter)
  var enumerable = Enumerable.Empty<Countries>().AsQueryable();
  var param = Expression.Parameter(typeof(Countries));
  if (filter != null)
    enumerable = (IQueryable<Countries>)filter.ApplyTo(enumerable, new ODataQuerySettings());
    // Exception : The query option is not bound to any CLR type. 'ApplyTo' is only supported with a query option bound to a CLR type.

    var mce = enumerable.Expression as MethodCallExpression;
    if (mce != null)
      var quote = mce.Arguments[1] as UnaryExpression;
      if (quote != null)
        return quote.Operand as Expression<Func<Countries, bool>>;
  return Expression.Lambda<Func<Countries, bool>>(Expression.Constant(true), param);

Sample code to create a simple database (solution and sql script)

Can anyone assist with this?

9/11/2014 7:09:41 PM

Accepted Answer

5/23/2017 10:26:23 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow