Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use switch for SqlServerDateOnlyMemberTranslator #34417

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -14,15 +14,6 @@ namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
/// </summary>
public class SqlServerDateOnlyMemberTranslator : IMemberTranslator
{
private static readonly Dictionary<string, string> DatePartMapping
= new()
{
{ nameof(DateOnly.Year), "year" },
{ nameof(DateOnly.Month), "month" },
{ nameof(DateOnly.DayOfYear), "dayofyear" },
{ nameof(DateOnly.Day), "day" }
};

private readonly ISqlExpressionFactory _sqlExpressionFactory;

/// <summary>
Expand All @@ -47,12 +38,27 @@ public SqlServerDateOnlyMemberTranslator(ISqlExpressionFactory sqlExpressionFact
MemberInfo member,
Type returnType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
=> member.DeclaringType == typeof(DateOnly) && DatePartMapping.TryGetValue(member.Name, out var datePart)
? _sqlExpressionFactory.Function(
"DATEPART",
new[] { _sqlExpressionFactory.Fragment(datePart), instance! },
nullable: true,
argumentsPropagateNullability: new[] { false, true },
returnType)
: null;
{
if (member.DeclaringType != typeof(DateOnly))
{
return null;
}

return member.Name switch
{
nameof(DateOnly.Year) => Function(instance, returnType, "year"),
nameof(DateOnly.Month) => Function(instance, returnType, "month"),
nameof(DateOnly.DayOfYear) => Function(instance, returnType, "dayofyear"),
nameof(DateOnly.Day) => Function(instance, returnType, "day"),
_ => null
};
}

private SqlExpression Function(SqlExpression? instance, Type returnType, string datePart)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: move this inside to be a local method

=> _sqlExpressionFactory.Function(
"DATEPART",
[_sqlExpressionFactory.Fragment(datePart), instance!],
nullable: true,
argumentsPropagateNullability: [false, true],
returnType);
}