'Using DbFunctions or EF.Function in Entity Framework Core 3.1.7

LINQ doesn't like DateTime functions Subtract and AddDays. So I was thinking to use DBFunction, I am using Entity Framework Core v3.1.7, but I can't access DateDiffDay() method.

According to this it's supported by Entity Framework Core v3.1.7 but when I try to access it DbFunctions only shows two options - Equal and ReferenceEquals:

enter image description here

Is there anything I need to enable to make other methods available? or Anything I need to make an change on my code to make it work with LINQ?

This is the query I need to replace with DateDiff because I keep getting an error

Could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().

My code:

var billing = _context.Billing.AsNoTracking()
                     .Select(i => new BillingSearchDTO
                                  {
                                      ID = i.ID,
                                      InvoiceNumber = i.InvoiceNumber,
                                      DaysPastDue = i.InvoiceDetail.Where(i => i.TypeID == TypeID_XT ||
                                                                               i.TypeID == TypeID_XP)
                                                                   .Select(i => i.AmountRemaining)
                                                                   .Sum() > 0 ? DateTime.Now.Subtract(i.Invoicedate.AddDays(i.ProductNav.GracePeriod.GetValueOrDefault())).Days : 0,.Days : 0
});

Error:

Select(i2 => i2.AmountRemaining) .Sum() > 0 ? DateTime.Now.Subtract(i.Outer.Invoicedate.AddDays((double)i.Inner.GracePeriod.GetValueOrDefault())).Days : 0 == 656)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information. at ...

Any Help?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source