Skip to content

Commit

Permalink
Allow CaseExpression with operand and else clause
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Jul 24, 2019
1 parent f40ddff commit 1d27fd8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/EFCore.Relational/Query/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,14 @@ public virtual SqlUnaryExpression Not(SqlExpression operand)
public virtual SqlUnaryExpression Negate(SqlExpression operand)
=> MakeUnary(ExpressionType.Negate, operand, operand.Type, operand.TypeMapping);

public virtual CaseExpression Case(SqlExpression operand, params CaseWhenClause[] whenClauses)
public virtual CaseExpression Case(SqlExpression operand, SqlExpression elseResult, params CaseWhenClause[] whenClauses)
{
var operandTypeMapping = operand.TypeMapping
?? whenClauses.Select(wc => wc.Test.TypeMapping).FirstOrDefault(t => t != null)
?? _typeMappingSource.FindMapping(operand.Type);
var resultTypeMapping = whenClauses.Select(wc => wc.Result.TypeMapping).FirstOrDefault(t => t != null);

var resultTypeMapping = elseResult?.TypeMapping
?? whenClauses.Select(wc => wc.Result.TypeMapping).FirstOrDefault(t => t != null);

operand = ApplyTypeMapping(operand, operandTypeMapping);

Expand All @@ -287,9 +289,14 @@ public virtual CaseExpression Case(SqlExpression operand, params CaseWhenClause[
ApplyTypeMapping(caseWhenClause.Result, resultTypeMapping)));
}

return new CaseExpression(operand, typeMappedWhenClauses);
elseResult = ApplyTypeMapping(elseResult, resultTypeMapping);

return new CaseExpression(operand, typeMappedWhenClauses, elseResult);
}

public virtual CaseExpression Case(SqlExpression operand, params CaseWhenClause[] whenClauses)
=> Case(operand, null, whenClauses);

public virtual CaseExpression Case(IReadOnlyList<CaseWhenClause> whenClauses, SqlExpression elseResult)
{
var resultTypeMapping = elseResult?.TypeMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public CaseExpression(
{
}

private CaseExpression(
public CaseExpression(
SqlExpression operand,
IReadOnlyList<CaseWhenClause> whenClauses,
SqlExpression elseResult)
Expand Down

0 comments on commit 1d27fd8

Please sign in to comment.