Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
""");
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ public virtual Task Select_anonymous_bool_constant_true(bool async)
ss => ss.Set<Customer>().Select(c => new { c.CustomerID, ConstantTrue = true }),
e => e.CustomerID);

[Theory, MemberData(nameof(IsAsyncData))]
public virtual Task Select_anonymous_non_numeric_constants(bool async)
Comment thread
Aziz-Dhiab marked this conversation as resolved.
Resolved
=> AssertQuery(
async,
ss => ss.Set<Customer>().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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading