src/Shared/EntityFramework/Core/Mapping/ViewGeneration/Structures/CaseStatement.cs
// <summary>
// Simplifies the <see cref="CaseStatement" /> so that unnecessary WHEN/THENs for nulls/undefined values are eliminated.
// Also, adds an ELSE clause if possible.
// </summary>
Simplifies the  so that unnecessary WHEN/THENs for nulls/undefined values are eliminated.
Also, adds an ELSE clause if possible.
Simplifies the so that unnecessary WHEN / THENs for nulls / undefined values are eliminated . Also adds an ELSE clause if possible .
internal void Simplify()
        {
            if (m_simplified)
            {
                return;
            }
            var clauses = new List<WhenThen>();
            var eliminatedNullClauses = false;
            foreach (var clause in m_clauses)
            {
                var constantSlot = clause.Value as ConstantProjectedSlot;
                if (constantSlot != null
                    && (constantSlot.CellConstant.IsNull() || constantSlot.CellConstant.IsUndefined()))
                {
                    eliminatedNullClauses = true;
                }
                else
                {
                    clauses.Add(clause);
                    if (clause.Condition.IsTrue)
                    {
                        break;
                    }
                }
            }
            if (eliminatedNullClauses && clauses.Count == 0)
            {
                m_elseValue = new ConstantProjectedSlot(Constant.Null);
            }
            if (clauses.Count > 0
                && false == eliminatedNullClauses)
            {
                var lastIndex = clauses.Count - 1;
                m_elseValue = clauses[lastIndex].Value;
                clauses.RemoveAt(lastIndex);
            }
            m_clauses = clauses;
            m_simplified = true;
        }
internal void Simplify ( ) { if ( m_simplified ) { return ; } var clauses = new List < WhenThen > ( ) ; var eliminatedNullClauses = false ; foreach ( var clause in m_clauses ) { var constantSlot = clause . Value as ConstantProjectedSlot ; if ( constantSlot != null && ( constantSlot . CellConstant . IsNull ( ) || constantSlot . CellConstant . IsUndefined ( ) ) ) { eliminatedNullClauses = true ; } else { clauses . Add ( clause ) ; if ( clause . Condition . IsTrue ) { break ; } } } if ( eliminatedNullClauses && clauses . Count == 0 ) { m_elseValue = new ConstantProjectedSlot ( Constant . Null ) ; } if ( clauses . Count > 0 && false == eliminatedNullClauses ) { var lastIndex = clauses . Count - 1 ; m_elseValue = clauses [ lastIndex ] . Value ; clauses . RemoveAt ( lastIndex ) ; } m_clauses = clauses ; m_simplified = true ; }
Simplifies the  so that unnecessary WHEN/THENs for nulls/undefined values are eliminated.
Simplifies the so that unnecessary WHEN / THENs for nulls / undefined values are eliminated .
// remove all WHEN clauses where the value gets set to "undefined" // We eliminate the last clause for now - we could determine the // "most complicated" WHEN clause and eliminate it // If null or undefined, remove it // none of subsequent case statements will be evaluated - ignore them // There is nothing left -- we should add a null as the value // If we eliminated some undefined or null clauses, we do not want an else clause // turn the last WHEN clause into an ELSE