From 5a097c67378435ba444c567c5cce07af04cb0fe8 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Tue, 17 Mar 2020 06:43:00 -0700 Subject: [PATCH] Query: Remove client eval code for EF.Functions methods Resolves #20294 --- .../SqlServerDbFunctionsExtensions.cs | 257 ++++-------------- .../NorthwindDbFunctionsQuerySqlServerTest.cs | 69 +---- 2 files changed, 57 insertions(+), 269 deletions(-) diff --git a/src/EFCore.SqlServer/Extensions/SqlServerDbFunctionsExtensions.cs b/src/EFCore.SqlServer/Extensions/SqlServerDbFunctionsExtensions.cs index a9355156abc..36cd2e77901 100644 --- a/src/EFCore.SqlServer/Extensions/SqlServerDbFunctionsExtensions.cs +++ b/src/EFCore.SqlServer/Extensions/SqlServerDbFunctionsExtensions.cs @@ -34,7 +34,7 @@ public static bool FreeText( [NotNull] string propertyReference, [NotNull] string freeText, int languageTerm) - => FreeTextCore(propertyReference, freeText, languageTerm); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FreeText))); /// /// @@ -52,9 +52,6 @@ public static bool FreeText( [CanBeNull] this DbFunctions _, [NotNull] string propertyReference, [NotNull] string freeText) - => FreeTextCore(propertyReference, freeText, null); - - private static bool FreeTextCore(string propertyName, string freeText, int? languageTerm) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FreeText))); /// @@ -75,7 +72,7 @@ public static bool Contains( [NotNull] string propertyReference, [NotNull] string searchCondition, int languageTerm) - => ContainsCore(propertyReference, searchCondition, languageTerm); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Contains))); /// /// @@ -93,9 +90,6 @@ public static bool Contains( [CanBeNull] this DbFunctions _, [NotNull] string propertyReference, [NotNull] string searchCondition) - => ContainsCore(propertyReference, searchCondition, null); - - private static bool ContainsCore(string propertyName, string searchCondition, int? languageTerm) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Contains))); /// @@ -110,7 +104,7 @@ public static int DateDiffYear( [CanBeNull] this DbFunctions _, DateTime startDate, DateTime endDate) - => endDate.Year - startDate.Year; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffYear))); /// /// Counts the number of year boundaries crossed between the startDate and endDate. @@ -124,9 +118,7 @@ public static int DateDiffYear( [CanBeNull] this DbFunctions _, DateTime? startDate, DateTime? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffYear(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffYear))); /// /// Counts the number of year boundaries crossed between the startDate and endDate. @@ -140,7 +132,7 @@ public static int DateDiffYear( [CanBeNull] this DbFunctions _, DateTimeOffset startDate, DateTimeOffset endDate) - => DateDiffYear(_, startDate.UtcDateTime, endDate.UtcDateTime); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffYear))); /// /// Counts the number of year boundaries crossed between the startDate and endDate. @@ -154,9 +146,7 @@ public static int DateDiffYear( [CanBeNull] this DbFunctions _, DateTimeOffset? startDate, DateTimeOffset? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffYear(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffYear))); /// /// Counts the number of month boundaries crossed between the startDate and endDate. @@ -170,7 +160,7 @@ public static int DateDiffMonth( [CanBeNull] this DbFunctions _, DateTime startDate, DateTime endDate) - => 12 * (endDate.Year - startDate.Year) + endDate.Month - startDate.Month; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMonth))); /// /// Counts the number of month boundaries crossed between the startDate and endDate. @@ -184,9 +174,7 @@ public static int DateDiffMonth( [CanBeNull] this DbFunctions _, DateTime? startDate, DateTime? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffMonth(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMonth))); /// /// Counts the number of month boundaries crossed between the startDate and endDate. @@ -200,7 +188,7 @@ public static int DateDiffMonth( [CanBeNull] this DbFunctions _, DateTimeOffset startDate, DateTimeOffset endDate) - => DateDiffMonth(_, startDate.UtcDateTime, endDate.UtcDateTime); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMonth))); /// /// Counts the number of month boundaries crossed between the startDate and endDate. @@ -214,9 +202,7 @@ public static int DateDiffMonth( [CanBeNull] this DbFunctions _, DateTimeOffset? startDate, DateTimeOffset? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffMonth(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMonth))); /// /// Counts the number of day boundaries crossed between the startDate and endDate. @@ -230,7 +216,7 @@ public static int DateDiffDay( [CanBeNull] this DbFunctions _, DateTime startDate, DateTime endDate) - => (endDate.Date - startDate.Date).Days; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffDay))); /// /// Counts the number of day boundaries crossed between the startDate and endDate. @@ -244,9 +230,7 @@ public static int DateDiffDay( [CanBeNull] this DbFunctions _, DateTime? startDate, DateTime? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffDay(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffDay))); /// /// Counts the number of day boundaries crossed between the startDate and endDate. @@ -260,7 +244,7 @@ public static int DateDiffDay( [CanBeNull] this DbFunctions _, DateTimeOffset startDate, DateTimeOffset endDate) - => DateDiffDay(_, startDate.UtcDateTime, endDate.UtcDateTime); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffDay))); /// /// Counts the number of day boundaries crossed between the startDate and endDate. @@ -274,9 +258,7 @@ public static int DateDiffDay( [CanBeNull] this DbFunctions _, DateTimeOffset? startDate, DateTimeOffset? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffDay(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffDay))); /// /// Counts the number of hour boundaries crossed between the startDate and endDate. @@ -290,12 +272,7 @@ public static int DateDiffHour( [CanBeNull] this DbFunctions _, DateTime startDate, DateTime endDate) - { - checked - { - return DateDiffDay(_, startDate, endDate) * 24 + endDate.Hour - startDate.Hour; - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffHour))); /// /// Counts the number of hour boundaries crossed between the startDate and endDate. @@ -309,9 +286,7 @@ public static int DateDiffHour( [CanBeNull] this DbFunctions _, DateTime? startDate, DateTime? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffHour(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffHour))); /// /// Counts the number of hour boundaries crossed between the startDate and endDate. @@ -325,7 +300,7 @@ public static int DateDiffHour( [CanBeNull] this DbFunctions _, DateTimeOffset startDate, DateTimeOffset endDate) - => DateDiffHour(_, startDate.UtcDateTime, endDate.UtcDateTime); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffHour))); /// /// Counts the number of hour boundaries crossed between the startDate and endDate. @@ -339,9 +314,7 @@ public static int DateDiffHour( [CanBeNull] this DbFunctions _, DateTimeOffset? startDate, DateTimeOffset? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffHour(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffHour))); /// /// Counts the number of hour boundaries crossed between the startTimeSpan and endTimeSpan. @@ -355,12 +328,7 @@ public static int DateDiffHour( [CanBeNull] this DbFunctions _, TimeSpan startTimeSpan, TimeSpan endTimeSpan) - { - checked - { - return endTimeSpan.Hours - startTimeSpan.Hours; - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffHour))); /// /// Counts the number of hour boundaries crossed between the startTimeSpan and endTimeSpan. @@ -374,9 +342,7 @@ public static int DateDiffHour( [CanBeNull] this DbFunctions _, TimeSpan? startTimeSpan, TimeSpan? endTimeSpan) - => (startTimeSpan.HasValue && endTimeSpan.HasValue) - ? (int?)DateDiffHour(_, startTimeSpan.Value, endTimeSpan.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffHour))); /// /// Counts the number of minute boundaries crossed between the startDate and endDate. @@ -390,12 +356,7 @@ public static int DateDiffMinute( [CanBeNull] this DbFunctions _, DateTime startDate, DateTime endDate) - { - checked - { - return DateDiffHour(_, startDate, endDate) * 60 + endDate.Minute - startDate.Minute; - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMinute))); /// /// Counts the number of minute boundaries crossed between the startDate and endDate. @@ -409,9 +370,7 @@ public static int DateDiffMinute( [CanBeNull] this DbFunctions _, DateTime? startDate, DateTime? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffMinute(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMinute))); /// /// Counts the number of minute boundaries crossed between the startDate and endDate. @@ -425,7 +384,7 @@ public static int DateDiffMinute( [CanBeNull] this DbFunctions _, DateTimeOffset startDate, DateTimeOffset endDate) - => DateDiffMinute(_, startDate.UtcDateTime, endDate.UtcDateTime); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMinute))); /// /// Counts the number of minute boundaries crossed between the startDate and endDate. @@ -439,9 +398,7 @@ public static int DateDiffMinute( [CanBeNull] this DbFunctions _, DateTimeOffset? startDate, DateTimeOffset? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffMinute(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMinute))); /// /// Counts the number of minute boundaries crossed between the startTimeSpan and endTimeSpan. @@ -455,12 +412,7 @@ public static int DateDiffMinute( [CanBeNull] this DbFunctions _, TimeSpan startTimeSpan, TimeSpan endTimeSpan) - { - checked - { - return DateDiffHour(_, startTimeSpan, endTimeSpan) * 60 + endTimeSpan.Minutes - startTimeSpan.Minutes; - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMinute))); /// /// Counts the number of minute boundaries crossed between the startTimeSpan and endTimeSpan. @@ -474,9 +426,7 @@ public static int DateDiffMinute( [CanBeNull] this DbFunctions _, TimeSpan? startTimeSpan, TimeSpan? endTimeSpan) - => (startTimeSpan.HasValue && endTimeSpan.HasValue) - ? (int?)DateDiffMinute(_, startTimeSpan.Value, endTimeSpan.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMinute))); /// /// Counts the number of second boundaries crossed between the startDate and endDate. @@ -490,12 +440,7 @@ public static int DateDiffSecond( [CanBeNull] this DbFunctions _, DateTime startDate, DateTime endDate) - { - checked - { - return DateDiffMinute(_, startDate, endDate) * 60 + endDate.Second - startDate.Second; - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffSecond))); /// /// Counts the number of second boundaries crossed between the startDate and endDate. @@ -509,9 +454,7 @@ public static int DateDiffSecond( [CanBeNull] this DbFunctions _, DateTime? startDate, DateTime? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffSecond(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffSecond))); /// /// Counts the number of second boundaries crossed between the startDate and endDate. @@ -525,7 +468,7 @@ public static int DateDiffSecond( [CanBeNull] this DbFunctions _, DateTimeOffset startDate, DateTimeOffset endDate) - => DateDiffSecond(_, startDate.UtcDateTime, endDate.UtcDateTime); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffSecond))); /// /// Counts the number of second boundaries crossed between the startDate and endDate. @@ -539,9 +482,7 @@ public static int DateDiffSecond( [CanBeNull] this DbFunctions _, DateTimeOffset? startDate, DateTimeOffset? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffSecond(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffSecond))); /// /// Counts the number of second boundaries crossed between the startTimeSpan and endTimeSpan. @@ -555,12 +496,7 @@ public static int DateDiffSecond( [CanBeNull] this DbFunctions _, TimeSpan startTimeSpan, TimeSpan endTimeSpan) - { - checked - { - return DateDiffMinute(_, startTimeSpan, endTimeSpan) * 60 + endTimeSpan.Seconds - startTimeSpan.Seconds; - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffSecond))); /// /// Counts the number of second boundaries crossed between the startTimeSpan and endTimeSpan. @@ -574,9 +510,7 @@ public static int DateDiffSecond( [CanBeNull] this DbFunctions _, TimeSpan? startTimeSpan, TimeSpan? endTimeSpan) - => (startTimeSpan.HasValue && endTimeSpan.HasValue) - ? (int?)DateDiffSecond(_, startTimeSpan.Value, endTimeSpan.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffSecond))); /// /// Counts the number of millisecond boundaries crossed between the startDate and endDate. @@ -590,12 +524,7 @@ public static int DateDiffMillisecond( [CanBeNull] this DbFunctions _, DateTime startDate, DateTime endDate) - { - checked - { - return DateDiffSecond(_, startDate, endDate) * 1000 + endDate.Millisecond - startDate.Millisecond; - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMillisecond))); /// /// Counts the number of millisecond boundaries crossed between the startDate and endDate. @@ -609,9 +538,7 @@ public static int DateDiffMillisecond( [CanBeNull] this DbFunctions _, DateTime? startDate, DateTime? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffMillisecond(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMillisecond))); /// /// Counts the number of millisecond boundaries crossed between the startDate and endDate. @@ -625,7 +552,7 @@ public static int DateDiffMillisecond( [CanBeNull] this DbFunctions _, DateTimeOffset startDate, DateTimeOffset endDate) - => DateDiffMillisecond(_, startDate.UtcDateTime, endDate.UtcDateTime); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMillisecond))); /// /// Counts the number of millisecond boundaries crossed between the startDate and endDate. @@ -639,9 +566,7 @@ public static int DateDiffMillisecond( [CanBeNull] this DbFunctions _, DateTimeOffset? startDate, DateTimeOffset? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffMillisecond(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMillisecond))); /// /// Counts the number of millisecond boundaries crossed between the startTimeSpan and endTimeSpan. @@ -655,12 +580,7 @@ public static int DateDiffMillisecond( [CanBeNull] this DbFunctions _, TimeSpan startTimeSpan, TimeSpan endTimeSpan) - { - checked - { - return DateDiffSecond(_, startTimeSpan, endTimeSpan) * 1000 + endTimeSpan.Milliseconds - startTimeSpan.Milliseconds; - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMillisecond))); /// /// Counts the number of millisecond boundaries crossed between the startTimeSpan and endTimeSpan. @@ -674,9 +594,7 @@ public static int DateDiffMillisecond( [CanBeNull] this DbFunctions _, TimeSpan? startTimeSpan, TimeSpan? endTimeSpan) - => (startTimeSpan.HasValue && endTimeSpan.HasValue) - ? (int?)DateDiffMillisecond(_, startTimeSpan.Value, endTimeSpan.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMillisecond))); /// /// Counts the number of microsecond boundaries crossed between the startDate and endDate. @@ -690,12 +608,7 @@ public static int DateDiffMicrosecond( [CanBeNull] this DbFunctions _, DateTime startDate, DateTime endDate) - { - checked - { - return (int)((endDate.Ticks - startDate.Ticks) / 10); - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMicrosecond))); /// /// Counts the number of microsecond boundaries crossed between the startDate and endDate. @@ -709,9 +622,7 @@ public static int DateDiffMicrosecond( [CanBeNull] this DbFunctions _, DateTime? startDate, DateTime? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffMicrosecond(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMicrosecond))); /// /// Counts the number of microsecond boundaries crossed between the startDate and endDate. @@ -725,7 +636,7 @@ public static int DateDiffMicrosecond( [CanBeNull] this DbFunctions _, DateTimeOffset startDate, DateTimeOffset endDate) - => DateDiffMicrosecond(_, startDate.UtcDateTime, endDate.UtcDateTime); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMicrosecond))); /// /// Counts the number of microsecond boundaries crossed between the startDate and endDate. @@ -739,9 +650,7 @@ public static int DateDiffMicrosecond( [CanBeNull] this DbFunctions _, DateTimeOffset? startDate, DateTimeOffset? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffMicrosecond(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMicrosecond))); /// /// Counts the number of microsecond boundaries crossed between the startTimeSpan and endTimeSpan. @@ -755,12 +664,7 @@ public static int DateDiffMicrosecond( [CanBeNull] this DbFunctions _, TimeSpan startTimeSpan, TimeSpan endTimeSpan) - { - checked - { - return (int)((endTimeSpan.Ticks - startTimeSpan.Ticks) / 10); - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMicrosecond))); /// /// Counts the number of microsecond boundaries crossed between the startTimeSpan and endTimeSpan. @@ -774,9 +678,7 @@ public static int DateDiffMicrosecond( [CanBeNull] this DbFunctions _, TimeSpan? startTimeSpan, TimeSpan? endTimeSpan) - => (startTimeSpan.HasValue && endTimeSpan.HasValue) - ? (int?)DateDiffMicrosecond(_, startTimeSpan.Value, endTimeSpan.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffMicrosecond))); /// /// Counts the number of nanosecond boundaries crossed between the startDate and endDate. @@ -790,12 +692,7 @@ public static int DateDiffNanosecond( [CanBeNull] this DbFunctions _, DateTime startDate, DateTime endDate) - { - checked - { - return (int)((endDate.Ticks - startDate.Ticks) * 100); - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffNanosecond))); /// /// Counts the number of nanosecond boundaries crossed between the startDate and endDate. @@ -809,9 +706,7 @@ public static int DateDiffNanosecond( [CanBeNull] this DbFunctions _, DateTime? startDate, DateTime? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffNanosecond(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffNanosecond))); /// /// Counts the number of nanosecond boundaries crossed between the startDate and endDate. @@ -825,7 +720,7 @@ public static int DateDiffNanosecond( [CanBeNull] this DbFunctions _, DateTimeOffset startDate, DateTimeOffset endDate) - => DateDiffNanosecond(_, startDate.UtcDateTime, endDate.UtcDateTime); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffNanosecond))); /// /// Counts the number of nanosecond boundaries crossed between the startDate and endDate. @@ -839,9 +734,7 @@ public static int DateDiffNanosecond( [CanBeNull] this DbFunctions _, DateTimeOffset? startDate, DateTimeOffset? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffNanosecond(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffNanosecond))); /// /// Counts the number of nanosecond boundaries crossed between the startTimeSpan and endTimeSpan. @@ -855,12 +748,7 @@ public static int DateDiffNanosecond( [CanBeNull] this DbFunctions _, TimeSpan startTimeSpan, TimeSpan endTimeSpan) - { - checked - { - return (int)((endTimeSpan.Ticks - startTimeSpan.Ticks) * 100); - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffNanosecond))); /// /// Counts the number of nanosecond boundaries crossed between the startTimeSpan and endTimeSpan. @@ -874,9 +762,7 @@ public static int DateDiffNanosecond( [CanBeNull] this DbFunctions _, TimeSpan? startTimeSpan, TimeSpan? endTimeSpan) - => (startTimeSpan.HasValue && endTimeSpan.HasValue) - ? (int?)DateDiffNanosecond(_, startTimeSpan.Value, endTimeSpan.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffNanosecond))); /// /// Counts the number of week boundaries crossed between the startDate and endDate. @@ -890,38 +776,7 @@ public static int DateDiffWeek( [CanBeNull] this DbFunctions _, DateTime startDate, DateTime endDate) - { - checked - { - var days = (endDate.Date - startDate.Date).Days; - var weeks = (int)days / 7; - var remainingDays = days % 7; - - if (remainingDays > 0) - { - var calendar = CultureInfo.InvariantCulture.Calendar; - - var first = calendar - .GetWeekOfYear( - startDate, - CalendarWeekRule.FirstFullWeek, - DayOfWeek.Sunday); - - var second = calendar - .GetWeekOfYear( - startDate.AddDays(remainingDays), - CalendarWeekRule.FirstFullWeek, - DayOfWeek.Sunday); - - if (first != second) - { - weeks++; - } - } - - return weeks; - } - } + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffWeek))); /// /// Counts the number of week boundaries crossed between the startDate and endDate. @@ -935,9 +790,7 @@ public static int DateDiffWeek( [CanBeNull] this DbFunctions _, DateTime? startDate, DateTime? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffWeek(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffWeek))); /// /// Counts the number of week boundaries crossed between the startDate and endDate. @@ -951,7 +804,7 @@ public static int DateDiffWeek( [CanBeNull] this DbFunctions _, DateTimeOffset startDate, DateTimeOffset endDate) - => DateDiffWeek(_, startDate.UtcDateTime, endDate.UtcDateTime); + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffWeek))); /// /// Counts the number of week boundaries crossed between the startDate and endDate. @@ -965,9 +818,7 @@ public static int DateDiffWeek( [CanBeNull] this DbFunctions _, DateTimeOffset? startDate, DateTimeOffset? endDate) - => (startDate.HasValue && endDate.HasValue) - ? (int?)DateDiffWeek(_, startDate.Value, endDate.Value) - : null; + => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(DateDiffWeek))); /// /// Validate if the given string is a valid date. diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindDbFunctionsQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindDbFunctionsQuerySqlServerTest.cs index 49bd158d2c6..6510225af4f 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindDbFunctionsQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindDbFunctionsQuerySqlServerTest.cs @@ -548,79 +548,16 @@ public virtual void DateDiff_Week_parameters_null() var count = context.Orders .Count(c => EF.Functions.DateDiffWeek( null, - new DateTimeOffset(1998, 5, 6, 0, 0, 0, TimeSpan.Zero)) == 5); + c.OrderDate) == 5); Assert.Equal(0, count); AssertSql( - @"@__p_0='False' - -SELECT COUNT(*) -FROM [Orders] AS [o] -WHERE @__p_0 = CAST(1 AS bit)"); - } - - [ConditionalFact] - public virtual void DateDiff_Week_server_vs_client_eval_datetime() - { - using var context = CreateContext(); - var endDate = new DateTime(1998, 5, 6, 0, 0, 0); - - var orders = context.Orders - .OrderBy(p => p.OrderID) - .Take(200) - .Select(c => new - { - Weeks = EF.Functions.DateDiffWeek(c.OrderDate, endDate), - c.OrderDate - }); - - foreach (var order in orders) - { - var weeks = EF.Functions.DateDiffWeek(order.OrderDate, endDate); - - Assert.Equal(weeks, order.Weeks); - } - - AssertSql( - @"@__p_0='200' -@__endDate_2='1998-05-06T00:00:00' (Nullable = true) (DbType = DateTime) - -SELECT TOP(@__p_0) DATEDIFF(WEEK, [o].[OrderDate], @__endDate_2) AS [Weeks], [o].[OrderDate] + @"SELECT COUNT(*) FROM [Orders] AS [o] -ORDER BY [o].[OrderID]"); +WHERE DATEDIFF(WEEK, NULL, [o].[OrderDate]) = 5"); } - [ConditionalFact] - public virtual void DateDiff_Week_server_vs_client_eval_datetimeoffset() - { - using var context = CreateContext(); - var endDate = new DateTimeOffset(1998, 5, 6, 0, 0, 0, TimeSpan.Zero); - - var orders = context.Orders - .OrderBy(p => p.OrderID) - .Take(200) - .Select(c => new - { - Weeks = EF.Functions.DateDiffWeek(c.OrderDate, endDate), - c.OrderDate - }); - - foreach (var order in orders) - { - var weeks = EF.Functions.DateDiffWeek(order.OrderDate, endDate); - - Assert.Equal(weeks, order.Weeks); - } - - AssertSql( - @"@__p_0='200' -@__endDate_2='1998-05-06T00:00:00.0000000+00:00' (Nullable = true) - -SELECT TOP(@__p_0) DATEDIFF(WEEK, CAST([o].[OrderDate] AS datetimeoffset), @__endDate_2) AS [Weeks], [o].[OrderDate] -FROM [Orders] AS [o] -ORDER BY [o].[OrderID]"); - } [ConditionalFact] public virtual void IsDate_not_valid() {