- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3.3k
Open
Milestone
Description
In NullCheckRemovingExpressionVisitor we have logic to simplify expressions like:
a == null ? null : a.Foo
into simple property access when on relational (null propagation is automatic)
However for some complex cases we miss the optimization and produce overly complicated.
e.g.:
ctx.Points.Select(
                    e => new
                    {
                        e.Id,
                        Distance = e.Point == null ? (double?)null : new Point(0, 1).Distance(e.Point)
                    })
produces:
SELECT ""p"".""Id"", CASE
    WHEN ""p"".""Point"" IS NULL THEN NULL
    ELSE Distance(GeomFromText('POINT (0 1)'), ""p"".""Point"")
END AS ""Distance""
FROM ""PointEntity"" AS ""p""rather than expected:
SELECT ""e"".""Id"", Distance(GeomFromText('POINT (0 1)'), ""e"".""Point"") AS ""Distance""
FROM ""PointEntity"" AS ""e""MaxG117