@@ -19,17 +19,6 @@ namespace EntityFrameworkCore.Jet.Query.ExpressionTranslators.Internal;
1919/// </remarks>
2020public class JetDateOnlyMemberTranslator ( ISqlExpressionFactory sqlExpressionFactory ) : IMemberTranslator
2121{
22- private static readonly Dictionary < string , string > DatePartMapping
23- = new ( )
24- {
25- { nameof ( DateTime . Year ) , "yyyy" } ,
26- { nameof ( DateTime . Month ) , "m" } ,
27- { nameof ( DateTime . DayOfYear ) , "y" } ,
28- { nameof ( DateTime . Day ) , "d" }
29- } ;
30-
31- private readonly ISqlExpressionFactory _sqlExpressionFactory = sqlExpressionFactory ;
32-
3322 /// <summary>
3423 /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
3524 /// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -41,12 +30,39 @@ private static readonly Dictionary<string, string> DatePartMapping
4130 MemberInfo member ,
4231 Type returnType ,
4332 IDiagnosticsLogger < DbLoggerCategory . Query > logger )
44- => member . DeclaringType == typeof ( DateOnly ) && DatePartMapping . TryGetValue ( member . Name , out var datePart )
45- ? _sqlExpressionFactory . Function (
33+ {
34+ if ( member . DeclaringType != typeof ( DateOnly ) || instance is null )
35+ {
36+ return null ;
37+ }
38+
39+ return member . Name switch
40+ {
41+ nameof ( DateOnly . Year ) => DatePart ( "yyyy" ) ,
42+ nameof ( DateOnly . Month ) => DatePart ( "m" ) ,
43+ nameof ( DateOnly . DayOfYear ) => DatePart ( "y" ) ,
44+ nameof ( DateOnly . Day ) => DatePart ( "d" ) ,
45+
46+ nameof ( DateOnly . DayNumber ) => sqlExpressionFactory . Add ( sqlExpressionFactory . Function (
47+ "DATEDIFF" ,
48+ [
49+ sqlExpressionFactory . Constant ( "d" ) ,
50+ sqlExpressionFactory . Constant ( new DateOnly ( 100 , 1 , 1 ) ) ,
51+ instance
52+ ] ,
53+ nullable : true ,
54+ argumentsPropagateNullability : [ false , true , true ] ,
55+ returnType ) , sqlExpressionFactory . Constant ( new DateOnly ( 100 , 1 , 1 ) . DayNumber ) ) ,
56+
57+ _ => null
58+ } ;
59+
60+ SqlExpression DatePart ( string datePart )
61+ => sqlExpressionFactory . Function (
4662 "DATEPART" ,
47- [ _sqlExpressionFactory . Constant ( datePart ) , instance ! ] ,
63+ [ sqlExpressionFactory . Constant ( datePart ) , instance ] ,
4864 nullable : true ,
4965 argumentsPropagateNullability : [ false , true ] ,
50- returnType )
51- : null ;
66+ returnType ) ;
67+ }
5268}
0 commit comments