I tried to use the SqlServerDbFunctionsExtensions.DateDiffDay but DbFunctions _ says
CS0119: DbFunctions is a type which is not valid in the given context
SqlServerDbFunctionsExtensions.DateDiffDay(this Microsoft.EntityFrameworkCore.DbFunctions _, DateTime.Now, i.Invoicedate) i.ProductNav.GracePeriod.GetValueOrDefault()
I also tried the following but i get the same error
SqlServerDbFunctionsExtensions.DateDiffDay(DbFunctions _, DateTime.Now, i.Invoicedate) i.ProductNav.GracePeriod.GetValueOrDefault()
CodePudding user response:
Provides CLR methods that get translated to database functions when used in LINQ to Entities queries. Calling these methods in other contexts (e.g. LINQ to Objects) will throw a NotSupportedException.
You can only access it in linq to entity queries, you can't do what you're trying to do.
it's an extension method you can't pass it in.
public static int DateDiffDay (this Microsoft.EntityFrameworkCore.DbFunctions _, DateTime startDate, DateTime endDate);
on DateDiffDay
you use it like this DateDiffDay(starDate, EndDate) in the query
var allDaysDifferences = myEntities.Sum(r => EF.Functions.DateDiffDay(r.FromDate, r.ToDate));
CodePudding user response:
DateDiffDay is an extension method. You call it with object instance and then pass the parameters without that object as a first parameter. In your case:
EF.Functions.DateDiffDay(DateTime.Now, i.InvoiceDate) i.ProductNav.GracePeriod.GetValueOrDefault();
You may need to add the using directive:
using Microsoft.EntityFrameworkCore;
and the needed nuget package
