DelegateDecompiler: Can't get DelegateDecompiler to play with DateTime

I absolutely love the concept, but I can’t get it to work with comparing DateTimes, something that I need to do a lot.

I’ve tried

[Computed]
public bool IsOverdue
{
   return this.FinishedAt.HasValue && this.FinishedAt.Value < DateTime.Now.AddDays(-1);
}

but it throws

LINQ to Entities does not recognize the method ‘System.DateTime AddDays(Double)’ method, and this method cannot be translated into a store expression.

I’ve tried using DbFunctions:

[Computed]
public bool IsOverdue
{
   return this.FinishedAt.HasValue && this.FinishedAt.Value < DbFunctions.AddDays(DateTime.Now, -1)
}

But it throws

Unable to cast object of type ‘System.Reflection.RuntimeConstructorInfo’ to type ‘System.Reflection.MethodInfo’.

Is there any way I can get a property like this to work, or am I out of luck?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 20 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Pushed to NuGet

Fixed in 9e114ff

If you can’t get it to work, can you create a demo project to demonstrate the issue?

Hi @Heras

  • create a local var holding the new date

This will not work as the variable will be inlined into the expression.

  • use DbFunctions

This works in the following way:

DbFunctions.AddYears(DateTime.Now, -10).Value

or

(DateTime) DbFunctions.AddYears(DateTime.Now, -10)

The .Value/cast is required because the DelegateDecompiler does not yet support optimizing folding of nullable value in this case.

I’m at v.0.19.0 already, but I did notice I have some nuget packages that have updates. I’ll try again when I have every package up-to-date