protected override Expression VisitConditional(ConditionalExpression c) { var test = Visit(c.Test); var ifTrue = Visit(c.IfTrue); var ifFalse = Visit(c.IfFalse); if (test.NodeType.In(ExpressionType.Equal, ExpressionType.NotEqual)) { var binaryExpression = (BinaryExpression)test; var left = binaryExpression.Left.StripCasts(); var right = binaryExpression.Right.StripCasts(); if ((IsEntity(left) && IsNull(right)) || (IsEntity(right) && IsNull(left))) { var nullPart = c.Test.NodeType == ExpressionType.Equal ? ifTrue : ifFalse; var memberAccessPart = c.Test.NodeType == ExpressionType.Equal ? ifFalse : ifTrue; if (IsNull(nullPart) && memberAccessPart.StripCasts().NodeType == ExpressionType.MemberAccess) { var memberAccess = (MemberExpression)memberAccessPart.StripCasts(); if (ExpressionTree.Equals(memberAccess.Expression, IsNull(right) ? left : right)) { return(memberAccessPart); } } } } if (!ReferenceEquals(test, c.Test) || !ReferenceEquals(ifTrue, c.IfTrue) || !ReferenceEquals(ifFalse, c.IfFalse)) { return(Expression.Condition(test, ifTrue, ifFalse)); } return(c); }
private static void AssertExpressionEquals(LambdaExpression expectedExpression, LambdaExpression actualExpression) { var equals = ExpressionTree.Equals(actualExpression, expectedExpression); Assert.That(equals, "Expected expression '{0}' got '{1}'", expectedExpression.ToString(true), actualExpression.ToString(true)); }