public void OuterJoinLogicalRewriter_DoubleFieldKey()
        {
            // NOTE: some parameter names are hard coded into the optimizer

            TestDatabase db         = new TestDatabase();
            Expression   expression = CreateExpression(() =>
                                                       db.Members.SelectMany(m =>
                                                                             db.Groups
                                                                             .Where(g => g.Id == m.GroupId && m.GroupId2 == g.Id2)
                                                                             .DefaultIfEmpty(),
                                                                             (m, g) => new { Member = m, Group = g }));

            Expression expectedExpression = CreateExpression(() =>
                                                             db.Members.GroupJoin(
                                                                 db.Groups,
                                                                 outer => new Tuple <int?, int>(outer.GroupId, outer.GroupId2),
                                                                 inner => new Tuple <int?, int>(inner.Id, inner.Id2),
                                                                 (outer, inner) => JoinGroup.Create(outer, inner))
                                                             .SelectMany(
                                                                 group => group.Inner.DefaultIfEmpty(),
                                                                 (group, inner) => new { Member = group.Outer, Group = inner }));

            IExpressionRewriter rewriter = new OuterJoinLogicalRewriter();

            expression = rewriter.Rewrite(expression);

            Assert.AreEqual(expectedExpression.Type, expression.Type);
            Assert.AreEqual(expectedExpression.ToString(), expression.ToString());
        }
        public void OuterJoinLogicalRewriter_TripleJoin()
        {
            // NOTE: some parameter names are hard coded into the optimizer

            TestDatabase db         = new TestDatabase();
            Expression   expression = CreateExpression(() =>
                                                       db.Members.SelectMany(outer =>
                                                                             db.Groups
                                                                             .Where(g => g.Id == outer.GroupId)
                                                                             .DefaultIfEmpty(),
                                                                             (outer, inner) =>
                                                                             new
            {
                Member = outer,
                Group  = inner
            })
                                                       .SelectMany(outer =>
                                                                   db.Groups
                                                                   .Where(g => g.Id == outer.Member.GroupId)
                                                                   .DefaultIfEmpty(),
                                                                   (outer, inner) =>
                                                                   new
            {
                Member = outer.Member,
                Group1 = outer.Group,
                Group2 = inner
            })
                                                       .SelectMany(outer =>
                                                                   db.Groups
                                                                   .Where(g => g.Id == outer.Member.GroupId)
                                                                   .DefaultIfEmpty(),
                                                                   (outer, inner) =>
                                                                   new
            {
                Member = outer.Member,
                Group1 = outer.Group1,
                Group2 = outer.Group2,
                Group3 = inner
            }));

            Expression expectedExpression = CreateExpression(() =>
                                                             db.Members.GroupJoin(
                                                                 db.Groups,
                                                                 outer => outer.GroupId,
                                                                 inner => inner.Id,
                                                                 (outer, inner) => JoinGroup.Create(outer, inner))
                                                             .SelectMany(
                                                                 group => group.Inner.DefaultIfEmpty(),
                                                                 (group, inner) =>
                                                                 new
            {
                Member = group.Outer,
                Group  = inner
            })
                                                             .GroupJoin(
                                                                 db.Groups,
                                                                 outer => outer.Member.GroupId,
                                                                 inner => inner.Id,
                                                                 (outer, inner) => JoinGroup.Create(outer, inner))
                                                             .SelectMany(
                                                                 group => group.Inner.DefaultIfEmpty(),
                                                                 (group, inner) =>
                                                                 new
            {
                Member = group.Outer.Member,
                Group1 = group.Outer.Group,
                Group2 = inner
            })
                                                             .GroupJoin(
                                                                 db.Groups,
                                                                 outer => outer.Member.GroupId,
                                                                 inner => inner.Id,
                                                                 (outer, inner) => JoinGroup.Create(outer, inner))
                                                             .SelectMany(
                                                                 group => group.Inner.DefaultIfEmpty(),
                                                                 (group, inner) =>
                                                                 new
            {
                Member = group.Outer.Member,
                Group1 = group.Outer.Group1,
                Group2 = group.Outer.Group2,
                Group3 = inner
            }));

            IExpressionRewriter rewriter = new OuterJoinLogicalRewriter();

            expression = rewriter.Rewrite(expression);

            Assert.AreEqual(expectedExpression.Type, expression.Type);
            Assert.AreEqual(expectedExpression.ToString(), expression.ToString());
        }