Reputation: 349
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()
Upvotes: 1
Views: 1468
Reputation: 74595
The other answers are what you would ordinarily do but I think it worth pointing out that where they imply an extension method is some sort of magical thing that can only be called in a certain way, that isn't really true
Nothing stops you calling an extension method as if it were any normal static method; you just have to provide some instance to call it on as the first argument
Here is an extension method:
public static string NoL(this string x){
return x.Replace("l","");
}
Note the this
on the first argument, making it an extension. You can either call it "on a string" or pass it a string:
var hw = "Hello World";
var heoword1 = hw.NoL());
var heoword2 = Ext.NoL(hw);
In probably all cases you'd do the first form, because static method extensions exist to make the code more compact and readable, but there's technically nothing wrong with your attempt to call the method you did, you just didn't provide a valid first parameter. This would work:
SqlServerDbFunctionsExtensions.DateDiffDay(EF.Functions, DateTime.Now, i.Invoicedate)
EF.Functions being the instance you would ordinarily just call DateDiff on:
EF.Functions.DateDiff(DateTime.Now, i.Invoicedate)
--
So when would you use an extension method like a normal one? Probably not many places, but it may make sense if its signature exactly matches some method that accepts a delegate.
For example, suppose we had an extension method:
public static class Exts{
public static string CombineStringAndInt(this string s, int i){
return s+i;
}
}
It takes a string and an int. LINQ's Select
has an overload that accepts a delegate that has arguments of string,int
. You could call the method above like this:
var arr = new[]{"a","b"};
arr.Select((s, i) => s.CombineStringAndInt(i));
But nothing prevents you calling it like this:
arr.Select(Exts.CombineStringAndInt);
Because it is a method that accepts a string and int, which is what is demanded by the Select
overload that accepts a delegate for a list item and the index it's at
There's nothing magical or weird about the extension method that prevents it being caled like this, or limiting it to only being used in a instance.ExtensionMethod(arg2,arg3..)
style
Upvotes: 1
Reputation: 3496
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));
Upvotes: 3
Reputation: 549
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
Upvotes: 2