zzzprojects / Eval-Expression.NET

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

Which Date Format Should We Use In WhereDynamic #124

Closed gksbr closed 2 years ago

gksbr commented 2 years ago

Hello,

Below code throws "No applicable member has been found for the expression" error.

WhereDynamic(x => "x.EndDate == 2022-01-01")

I tried various date and time formats but I couldn't figure it out.

If I try like this, it works, but I want to query with day, month and year.

WhereDynamic(x => "x.EndDate.Year == 2022")

Could you help me please?

Thank you.

JonathanMagnan commented 2 years ago

Hello @gksbr ,

One way to work easily with datetime is surrounding them with #.

So something like this:

WhereDynamic(x => "x.EndDate == #2022-01-01#")

Here is an online example: https://dotnetfiddle.net/21dOz9

// Date
Console.WriteLine(Eval.Execute("#2020-01-01#"));

// DateTime
Console.WriteLine(Eval.Execute("#2020-01-01 01:02:03#"));

// TimeSpan
Console.WriteLine(Eval.Execute("##01:02:03##"));

Let me know if that worked.

Best Regards,

Jon

gksbr commented 2 years ago

Hello @JonathanMagnan,

It worked, thank you so much for the help