LINQKit icon indicating copy to clipboard operation
LINQKit copied to clipboard

InvalidCastException PropertyExpression -> LambdaExpression

Open freica opened this issue 6 years ago • 4 comments

I am using the following code:


        public static Func<T1, bool> StatusOffen
        {
            get { return d => d.status < 400; }
        }
        public void Test()
        {
                Expression<Func<T1, bool>> expr1 = a => Item.StatusOffen(a);
                Expression<Func<T1, bool>> expr2 = a => Item.StatusBearbeitbar(a);
                Expression<Func<T1, bool>> expr3 = a => Item.StatusKeinVersand(a);
                var predicate = PredicateBuilder.False<T1>();
                predicate = predicate.Or(expr1);
                predicate = predicate.Or(expr2);
                predicate = predicate.Or(expr3);
               
                ////predicate = predicate.Or(a => a.status < 400);
                ////predicate = predicate.Or(a => a.status == 850);
                ////predicate = predicate.Or(a => a.status == 400);

                IQueryable<T1> query1 = this.GetByFilterObjectInternal(entities);
                query1 = query1.AsExpandable().Where(predicate);
                ....
        }

On calling the where statement I get the invalid cast Exception. If I use the commented variant instead all runs well. So what should I do to use the funcs? Best regards freica

freica avatar Feb 01 '19 12:02 freica

this already has a solution in here

but I don't know why still it's not fixed in the source code. @scottksmith95 @StefH is there any special reason the method and property expressions support still it's not added to the source?

mhamri avatar Apr 03 '19 07:04 mhamri

I actually forked this repo to apply fix from that SO thread only to find that it's actually already fixed and works fine. It doesn't work in OPs scenario because the property invoked should return Expression<Func<...>>, not just Func<...>.

a-stankevich avatar Apr 29 '19 06:04 a-stankevich

I encountered the same issue. Has anyone actually filled a pull request to fix this? I'll look around if the issue is actually identical and will file a PR to close this issue.

Bert-Proesmans avatar Sep 23 '19 15:09 Bert-Proesmans

Seems I cannot reproduce my issue which resembles the provided code.. but with Expression<Func<..>> instead of simply Func<>. I'll need to take a fresh look at my code to properly find out I did something stupid.

Bert-Proesmans avatar Sep 24 '19 18:09 Bert-Proesmans