3

first time I've not managed to find an existing answer to my problem.

Basically I have a generic expression to allow filtering active items on an IQueryable usage:

queryableGoesHere.ActiveAtItems(x => x.StartDate, x => x.EndDate, asAtDate, true)

Implementation of expression:

        public static Expression<Func<T, Func<T, DateTime>, Func<T, DateTime?>, DateTime?, bool?, bool>> GetActiveAtItemsExprGeneric<T>()
        where T : class
    {
        return (o, from, to, givenTime, isActive) => (!to(o).HasValue);
    }

I've simplified the return line to highlight the problem. It works untill I attempt to use either of the two funcs, in this case 'to(o)'.

It fails when using linqkit here:

return (from m in source.AsExpandable()
                where predicate.Invoke(m, arg1, arg2, arg3, arg4)
                select m);

With the exception "Additional information: Unable to cast object of type 'System.Linq.Expressions.TypedParameterExpression' to type 'System.Linq.Expressions.LambdaExpression'."

Now I've never passed a func and used it in an expression in this way, and my google-fu has failed to find any reference to this exception. I'm unsure if what I'm doing is even possible... Any help would be much appreciated!

Drift
  • 31
  • 1
  • I think the problem are the two `Func<..>` arguments. Try changing them to `Expression>` and use `Invoke` inside, e.g. `to.Invoke(o)` – Ivan Stoev Sep 08 '16 at 07:06
  • Thanks Ivan. I changed the signature to `Expression>, Expression>` and the call now uses `(!to.Invoke(o).HasValue);`. However still receiving the same error :( – Drift Sep 08 '16 at 08:44
  • Hmm, too bad:( Can't you change `GetActiveAtItemsExprGeneric` to actually have arguments and just return `Expression>` that you can later just `Invoke(m)`? – Ivan Stoev Sep 08 '16 at 11:29
  • Yep, thanks Ivan, was following the pattern we've been using for these expressions. However you're right, doing that seems to have solved the problem :) – Drift Sep 09 '16 at 01:57

0 Answers0