Expression conversion RRS feed

  • Question

  • I am still a little green to Expressions and having difficulty figuring this problem out.  This may be more of a Lambda expression issue over EF, but I am hoping someone can atleast point me in the correct direction:

    I am attempting to do the following:


    internalstaticIQueryable<TSource> WithSecurity<TSource>(thisIQueryable<TSource> source, Expression<Func<Security.Access, TSource, bool>> predicate, params MyRoles[] roles)         {             DL.MyContext ctx = Csla.Data.DbContextManager<DL.MyContext>.GetManager().DbContext;             var accessData = ctx.Access.Where(e => roles.Contains((MyRoles)e.Role_ID));             Expression x = predicate asLambdaExpression;             source =                 from c in source                 where accessData.Any(predicate)                 select c;             return source;         }

    On the where clause, there is clearly a problem as the predicate is of type Expression<Func<Security.Access, TSource, bool>>, but the Any is expecting Expression<Func<Security.Access, bool>>. Any assistance on how to convert will be greatly appreciated.


    Thursday, September 20, 2012 2:27 PM


  • In case someone is looking for the answer, a solution was given on Stack Overflow:

    var compiledPredicate = predicate.Compile();

    source.Where(s => accessData.Any(ad => compiledPredicate(ad, s)));

    The expression must be compiled.  After it is compiled, you can pass values to the resulting delegate.

    • Marked as answer by Joseph Morin Friday, September 21, 2012 2:14 PM
    Friday, September 21, 2012 2:14 PM