1
+ using RepoDb . Enumerations ;
2
+ using RepoDb . Exceptions ;
3
+ using RepoDb . Extensions ;
4
+ using RepoDb . Interfaces ;
5
+ using RepoDb . Resolvers ;
1
6
using System ;
2
7
using System . Collections . Generic ;
3
8
using System . Data ;
4
9
using System . Data . Common ;
5
10
using System . Linq ;
6
11
using System . Linq . Expressions ;
7
12
using System . Reflection ;
8
- using RepoDb . Enumerations ;
9
- using RepoDb . Exceptions ;
10
- using RepoDb . Extensions ;
11
- using RepoDb . Interfaces ;
12
- using RepoDb . Resolvers ;
13
13
14
14
namespace RepoDb . Reflection
15
15
{
@@ -510,7 +510,15 @@ internal static MethodInfo GetDateTimeTimeOfDayPropertyGetMethod() =>
510
510
/// <returns></returns>
511
511
internal static MethodInfo GetDateOnlyFromDateTimeStaticMethod ( ) =>
512
512
StaticType . DateOnly . GetMethod ( "FromDateTime" ) ;
513
+
514
+ /// <summary>
515
+ ///
516
+ /// </summary>
517
+ /// <returns></returns>
518
+ internal static MethodInfo GetDateTimeFromDateOnlyMethod ( ) =>
519
+ StaticType . DateOnly . GetMethod ( "ToDateTime" , new Type [ ] { StaticType . TimeOnly } ) ;
513
520
#endif
521
+
514
522
/// <summary>
515
523
///
516
524
/// </summary>
@@ -548,6 +556,24 @@ internal static Expression ConvertExpressionToNullableGetValueOrDefaultExpressio
548
556
return expression ;
549
557
}
550
558
559
+ #if NET6_0_OR_GREATER
560
+ internal static Expression ConvertExpressionToNullableGetValueOrDefaultExpression ( Func < Expression , Expression > converter , Expression expression )
561
+ {
562
+ if ( Nullable . GetUnderlyingType ( expression . Type ) != null )
563
+ {
564
+ var converted = converter ( ConvertExpressionToNullableGetValueOrDefaultExpression ( expression ) ) ;
565
+ var nullableType = typeof ( Nullable < > ) . MakeGenericType ( converted . Type ) ;
566
+ return Expression . Condition (
567
+ Expression . Property ( expression , nameof ( Nullable < int > . HasValue ) ) ,
568
+ Expression . Convert ( converted , nullableType ) ,
569
+ Expression . Constant ( null , nullableType )
570
+ ) ;
571
+ }
572
+
573
+ return converter ( expression ) ;
574
+ }
575
+ #endif
576
+
551
577
/// <summary>
552
578
///
553
579
/// </summary>
@@ -601,7 +627,15 @@ internal static Expression ConvertExpressionToDateTimeToTimeSpanExpression(Expre
601
627
/// <param name="expression"></param>
602
628
/// <returns></returns>
603
629
internal static Expression ConvertExpressionToDateTimeToDateOnlyExpression ( Expression expression ) =>
604
- ConvertExpressionToNullableGetValueOrDefaultExpression ( ConvertExpressionToDateOnlyFromDateTimeExpression ( expression ) ) ;
630
+ ConvertExpressionToNullableGetValueOrDefaultExpression ( ConvertExpressionToDateTimeFromDateOnlyExpression , expression ) ;
631
+
632
+ /// <summary>
633
+ ///
634
+ /// </summary>
635
+ /// <param name="expression"></param>
636
+ /// <returns></returns>
637
+ internal static Expression ConvertExpressionToDateOnlyToDateTimeExpression ( Expression expression ) =>
638
+ ConvertExpressionToNullableGetValueOrDefaultExpression ( ConvertExpressionToDateOnlyFromDateTimeExpression , expression ) ;
605
639
#endif
606
640
/// <summary>
607
641
///
@@ -625,6 +659,14 @@ internal static Expression ConvertExpressionToDateTimeTimeOfDayExpression(Expres
625
659
/// <param name="expression"></param>
626
660
/// <returns></returns>
627
661
internal static Expression ConvertExpressionToDateOnlyFromDateTimeExpression ( Expression expression ) =>
662
+ Expression . Call ( expression , GetDateTimeFromDateOnlyMethod ( ) , Expression . Constant ( default ( TimeOnly ) ) ) ;
663
+
664
+ /// <summary>
665
+ ///
666
+ /// </summary>
667
+ /// <param name="expression"></param>
668
+ /// <returns></returns>
669
+ internal static Expression ConvertExpressionToDateTimeFromDateOnlyExpression ( Expression expression ) =>
628
670
Expression . Call ( null , GetDateOnlyFromDateTimeStaticMethod ( ) , expression ) ;
629
671
#endif
630
672
/// <summary>
@@ -907,6 +949,12 @@ internal static Expression ConvertExpressionWithAutomaticConversion(Expression e
907
949
{
908
950
expression = ConvertExpressionToDateTimeToDateOnlyExpression ( expression ) ;
909
951
}
952
+
953
+ // DateOnly to DateTime
954
+ else if ( fromType = = StaticType . DateOnly && toType == StaticType . DateTime )
955
+ {
956
+ expression = ConvertExpressionToDateOnlyToDateTimeExpression ( expression ) ;
957
+ }
910
958
#endif
911
959
// Others
912
960
else
@@ -1084,7 +1132,7 @@ internal static Expression ConvertExpressionToClassHandlerSetExpression(Expressi
1084
1132
return entityOrEntitiesExpression ;
1085
1133
}
1086
1134
1087
- #endregion
1135
+ #endregion
1088
1136
1089
1137
#region Common
1090
1138
0 commit comments