zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
449 stars 86 forks source link

Flags enum problem #133

Closed toliverg closed 2 years ago

toliverg commented 2 years ago

Hi,

I encountered a problem trying to filter a dynamic linq expression using a flag-enum and a bit-and.

I can execute something like .WhereDynamic("x => x.CustomerType == CustomerType.A") but when I try to filter for something like: .WhereDynamic("x => (x.CustomerType & CustomerType.A) == CustomerType.A") I'm getting Z.Expressions.Compiler.Shared.EvalException: 'Oops! No applicable member has been found for the expression. The error occurred for expression "." at position 3 near ".Where(x => (x.CustomerTy".'

One workaround is WhereDynamic("x => ((int)x.CustomerType & (int)CustomerType.A) == (int)CustomerType.A"). I'm not sure if this fix is universal, specially when working with EF Core and things like that.

I have included a small solution to show you the problem.

Best regards, Tolo TestFlags.zip

JonathanMagnan commented 2 years ago

Hello @toliverg ,

Thank you for the project,

My developer will look at it and try to fix it.

Best Regards,

Jon

JonathanMagnan commented 2 years ago

Hello @toliverg ,

The v4.0;82 has been released.

Let me know if everything is now working correctly with enum.

Best Regards,

Jon

toliverg commented 2 years ago

Hi,

Yes, it's working fine now. Thanks a lot for the fast fix.

Best Regards,

Tolo

fdahlberg commented 1 year ago

Hi,

I'm getting this issue in version 5.0.9.0 when trying to run something like this:

var customers = context.Customers.WhereDynamic(x => "x.Status == CustomerStatus.IsActive && x.LastLogon >= DateTime.Now.AddMonths(-1)").ToList();

I just slightly modified your sample here; https://dotnetfiddle.net/b5EmTM

It seems like using an enum in the WhereDynamic doesn't work. Or maybe I'm missing something.

Kind Regards Fredrik Dahlberg

JonathanMagnan commented 1 year ago

Hello @fdahlberg ,

The current Fiddle you provided is currently working. Perhaps you modified it and saved it by mistake or my developer is missing something?

Best Regards,

Jon