From a5eb467c0e8e4c37a667216231e14d789d6de040 Mon Sep 17 00:00:00 2001 From: Aziz Date: Thu, 8 Oct 2026 04:03:27 +0100 Subject: [PATCH 1/3] fixes 35826 --- ...lationalProjectionBindingExpressionVisitor.cs | 13 +++++++++++++ .../Query/NorthwindSelectQueryTestBase.cs | 16 ++++++++++++++++ .../Query/NorthwindSelectQuerySqlServerTest.cs | 14 ++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/src/EFCore.Relational/Query/Internal/RelationalProjectionBindingExpressionVisitor.cs b/src/EFCore.Relational/Query/Internal/RelationalProjectionBindingExpressionVisitor.cs index db4fb1f7e60..722285aae0c 100644 --- a/src/EFCore.Relational/Query/Internal/RelationalProjectionBindingExpressionVisitor.cs +++ b/src/EFCore.Relational/Query/Internal/RelationalProjectionBindingExpressionVisitor.cs @@ -193,6 +193,9 @@ public virtual Expression Translate(SelectExpression selectExpression, Expressio case NewExpression or MemberInitExpression or StructuralTypeShaperExpression or IncludeExpression: return base.Visit(expression); + //case SqlConstantExpression: + // return expression; + case null: return null; @@ -341,6 +344,16 @@ public virtual Expression Translate(SelectExpression selectExpression, Expressio switch (_sqlTranslator.TranslateProjection(expression)) { case SqlExpression sqlExpression: + if (sqlExpression is SqlConstantExpression { Value: not null } + && (sqlExpression.Type == typeof(Guid) + || sqlExpression.Type == typeof(DateTime) + || sqlExpression.Type == typeof(DateOnly) + || sqlExpression.Type == typeof(TimeOnly) + || sqlExpression.Type == typeof(TimeSpan) + || sqlExpression.Type == typeof(DateTimeOffset))) + { + sqlExpression = new SqlUnaryExpression(ExpressionType.Convert, sqlExpression, sqlExpression.Type, sqlExpression.TypeMapping); + } _projectionMapping[_projectionMembers.Peek()] = sqlExpression; return new ProjectionBindingExpression( _selectExpression, _projectionMembers.Peek(), expression.Type.MakeNullable()); diff --git a/test/EFCore.Specification.Tests/Query/NorthwindSelectQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/NorthwindSelectQueryTestBase.cs index 2e0060f3202..9f2d21a466a 100644 --- a/test/EFCore.Specification.Tests/Query/NorthwindSelectQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/NorthwindSelectQueryTestBase.cs @@ -228,6 +228,22 @@ public virtual Task Select_anonymous_bool_constant_true(bool async) ss => ss.Set().Select(c => new { c.CustomerID, ConstantTrue = true }), e => e.CustomerID); + [Theory, MemberData(nameof(IsAsyncData))] + public virtual Task Select_anonymous_non_numeric_constants(bool async) + => AssertQuery( + async, + ss => ss.Set().Select(c => new + { + c.CustomerID, + ConstantGuid = Guid.Empty, + ConstantDateTime = DateTime.MinValue, + ConstantDateOnly = DateOnly.MinValue, + ConstantTimeSpan = TimeSpan.Zero, + ConstantTimeOnly = TimeOnly.MinValue, + ConstantDateTimeOffset = DateTimeOffset.MinValue + }), + e => e.CustomerID); + [Theory, MemberData(nameof(IsAsyncData))] public virtual Task Select_anonymous_constant_in_expression(bool async) => AssertQuery( diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindSelectQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindSelectQuerySqlServerTest.cs index 6541035a41e..d3cb8a7d8dd 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindSelectQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindSelectQuerySqlServerTest.cs @@ -219,6 +219,20 @@ FROM [Customers] AS [c] """); } + public override async Task Select_anonymous_non_numeric_constants(bool async) + { + await base.Select_anonymous_non_numeric_constants(async); + + AssertSql( + """ +@MinValue='01/01/0001' (DbType = Date) +@MinValue1='00:00' (DbType = Time) + +SELECT [c].[CustomerID], CAST('00000000-0000-0000-0000-000000000000' AS uniqueidentifier) AS [ConstantGuid], CAST('0001-01-01T00:00:00.0000000' AS datetime2) AS [ConstantDateTime], @MinValue AS [ConstantDateOnly], CAST('00:00:00' AS time) AS [ConstantTimeSpan], @MinValue1 AS [ConstantTimeOnly], CAST('0001-01-01T00:00:00.0000000+00:00' AS datetimeoffset) AS [ConstantDateTimeOffset] +FROM [Customers] AS [c] +"""); + } + public override async Task Select_anonymous_constant_in_expression(bool async) { await base.Select_anonymous_constant_in_expression(async); From 45bf1ed0152c21d1b87077cb24b8bcd93a7fd7cc Mon Sep 17 00:00:00 2001 From: Aziz Date: Thu, 8 Oct 2026 04:04:10 +0100 Subject: [PATCH 2/3] remove comment --- .../Internal/RelationalProjectionBindingExpressionVisitor.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/EFCore.Relational/Query/Internal/RelationalProjectionBindingExpressionVisitor.cs b/src/EFCore.Relational/Query/Internal/RelationalProjectionBindingExpressionVisitor.cs index 722285aae0c..973e7540845 100644 --- a/src/EFCore.Relational/Query/Internal/RelationalProjectionBindingExpressionVisitor.cs +++ b/src/EFCore.Relational/Query/Internal/RelationalProjectionBindingExpressionVisitor.cs @@ -193,9 +193,6 @@ public virtual Expression Translate(SelectExpression selectExpression, Expressio case NewExpression or MemberInitExpression or StructuralTypeShaperExpression or IncludeExpression: return base.Visit(expression); - //case SqlConstantExpression: - // return expression; - case null: return null; From eedfd5f73885819cbf2c4d3509185447bdb75094 Mon Sep 17 00:00:00 2001 From: Aziz Date: Thu, 8 Oct 2026 22:52:55 +0100 Subject: [PATCH 3/3] add cosmos override for the unit test added in a5eb467 --- .../Query/NorthwindSelectQueryCosmosTest.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindSelectQueryCosmosTest.cs b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindSelectQueryCosmosTest.cs index bafe659e6db..a7015ea5a5c 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindSelectQueryCosmosTest.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindSelectQueryCosmosTest.cs @@ -283,6 +283,30 @@ SELECT VALUE "ConstantTrue" : true } FROM root c +"""); + }); + + public override Task Select_anonymous_non_numeric_constants(bool async) + => Fixture.NoSyncTest( + async, async a => + { + await base.Select_anonymous_non_numeric_constants(a); + AssertSql( + """ +@MinValue='0001-01-01' +@MinValue1='00:00:00' + +SELECT VALUE +{ + "CustomerID" : c["id"], + "ConstantGuid" : "00000000-0000-0000-0000-000000000000", + "ConstantDateTime" : "0001-01-01T00:00:00", + "ConstantDateOnly" : @MinValue, + "ConstantTimeSpan" : "00:00:00", + "ConstantTimeOnly" : @MinValue1, + "ConstantDateTimeOffset" : "0001-01-01T00:00:00+00:00" +} +FROM root c """); });