//		internal static readonly AstNode wherePatternCase1 =
        //			new InvocationExpression(
        //				new MemberReferenceExpression(
        //					new InvocationExpression(
        //						new MemberReferenceExpression(new AnyNode("target"), "Where"),
        //						new LambdaExpression {
        //							Parameters = { PatternHelper.NamedParameter ("param1", PatternHelper.AnyType ("paramType", true), Pattern.AnyString) },
        //							Body = PatternHelper.OptionalParentheses (new IsExpression(new AnyNode("expr1"), new AnyNode("type")))
        //						}
        //					), "Select"),
        //				new LambdaExpression {
        //					Parameters = { PatternHelper.NamedParameter ("param2", PatternHelper.AnyType ("paramType", true), Pattern.AnyString) },
        //					Body = PatternHelper.OptionalParentheses (new AsExpression(PatternHelper.OptionalParentheses (new AnyNode("expr2")), new Backreference("type")))
        //				}
        //		);
        //
        //		internal static readonly AstNode wherePatternCase2 =
        //			new InvocationExpression(
        //				new MemberReferenceExpression(
        //					new InvocationExpression(
        //						new MemberReferenceExpression(new AnyNode("target"), "Where"),
        //						new LambdaExpression {
        //							Parameters = { PatternHelper.NamedParameter ("param1", PatternHelper.AnyType ("paramType", true), Pattern.AnyString) },
        //							Body = PatternHelper.OptionalParentheses (new IsExpression(PatternHelper.OptionalParentheses (new AnyNode("expr1")), new AnyNode("type")))
        //						}
        //					), "Select"),
        //				new LambdaExpression {
        //					Parameters = { PatternHelper.NamedParameter ("param2", PatternHelper.AnyType ("paramType", true), Pattern.AnyString) },
        //					Body = PatternHelper.OptionalParentheses (new CastExpression(new Backreference("type"), PatternHelper.OptionalParentheses (new AnyNode("expr2"))))
        //				}
        //		);
        static bool MatchWhereSelect(InvocationExpressionSyntax selectInvoke, out ExpressionSyntax target, out TypeSyntax type)
        {
            target = null;
            type = null;

            if (selectInvoke.ArgumentList.Arguments.Count != 1)
                return false;
            var anyInvokeBase = selectInvoke.Expression as MemberAccessExpressionSyntax;
            if (anyInvokeBase == null || anyInvokeBase.Name.Identifier.Text != "Select")
                return false;
            var whereInvoke = anyInvokeBase.Expression as InvocationExpressionSyntax;
            if (whereInvoke == null || whereInvoke.ArgumentList.Arguments.Count != 1)
                return false;
            var baseMember = whereInvoke.Expression as MemberAccessExpressionSyntax;
            if (baseMember == null || baseMember.Name.Identifier.Text != "Where")
                return false;
            target = baseMember.Expression;

            ParameterSyntax param1, param2;
            ExpressionSyntax expr1, expr2;
            if (!ExtractLambda(whereInvoke.ArgumentList.Arguments[0], out param1, out expr1))
                return false;
            if (!ExtractLambda(selectInvoke.ArgumentList.Arguments[0], out param2, out expr2))
                return false;
            if (!expr1.IsKind(SyntaxKind.IsExpression))
                return false;
            type = (expr1 as BinaryExpressionSyntax)?.Right as TypeSyntax;
            if (type == null)
                return false;
            if (expr2.IsKind(SyntaxKind.AsExpression))
            {
                if (!CompareNames(param2, (expr2 as BinaryExpressionSyntax).Left as IdentifierNameSyntax))
                    return false;
                if (!type.IsEquivalentTo((expr2 as BinaryExpressionSyntax)?.Right))
                    return false;
            }
            else if (expr2.IsKind(SyntaxKind.CastExpression))
            {
                if (!CompareNames(param2, (expr2 as CastExpressionSyntax)?.Expression.SkipParens() as IdentifierNameSyntax))
                    return false;
                if (!type.IsEquivalentTo((expr2 as CastExpressionSyntax)?.Type))
                    return false;
            }
            else
                return false;

            if (!CompareNames(param1, (expr1 as BinaryExpressionSyntax)?.Left as IdentifierNameSyntax))
                return false;


            return target != null;
        }