I have code that heavily depends on ISO calendar weeks; its persistence layer is done using the Entity Framework 6.
In C#, I have added an extension method to DateTime:
// From https://stackoverflow.com/a/11155102/112964
public static int IsoWeek(this DateTime self) {
var day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(self);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
self = self.AddDays(3);
}
// Return the week of our adjusted day
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(self, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
I would like for this extension method to work in IQueryable
s as well, and map it to the SQL Server function DATEPART(iso_week, self)
.
This means that I would like to call DATEPART
with a first parameter that is always 'iso_week'
. The second parameter should be the DateTime
passed into the C# method. From what I understand, DbFunctionAttribute
only works if both parameters are parameters of the function.
Is it possible to perform such a mapping or do I need to create a stored procedure that calls DATEPART(iso_week, @param)
and call that procedure?