bool TryRemoveTransparentIdentifier(QueryExpression query, QueryFromClause fromClause, QueryExpression innerQuery, Dictionary <string, object> letClauses)
        {
            if (!IsTransparentIdentifier(fromClause.Identifier))
            {
                return(false);
            }
            QuerySelectClause selectClause = innerQuery.Clauses.Last() as QuerySelectClause;
            Match             match        = selectTransparentIdentifierPattern.Match(selectClause);

            if (!match.Success)
            {
                return(false);
            }

            // from * in (from x in ... select new { members of anonymous type }) ...
            // =>
            // from x in ... { let x = ... } ...
            fromClause.Remove();
            selectClause.Remove();
            // Move clauses from innerQuery to query
            QueryClause insertionPos = null;

            foreach (var clause in innerQuery.Clauses)
            {
                query.Clauses.InsertAfter(insertionPos, insertionPos = clause.Detach());
            }

            foreach (var expr in match.Get <Expression>("expr"))
            {
                switch (expr)
                {
                case IdentifierExpression identifier:
                    // nothing to add
                    continue;

                case NamedExpression namedExpression:
                    if (namedExpression.Expression is IdentifierExpression identifierExpression && namedExpression.Name == identifierExpression.Identifier)
                    {
                        letClauses[namedExpression.Name] = identifierExpression.Annotation <ILVariableResolveResult>();
                        continue;
                    }
                    QueryLetClause letClause = new QueryLetClause {
                        Identifier = namedExpression.Name, Expression = namedExpression.Expression.Detach()
                    };
                    var annotation = new LetIdentifierAnnotation();
                    letClause.AddAnnotation(annotation);
                    letClauses[namedExpression.Name] = annotation;
                    query.Clauses.InsertAfter(insertionPos, letClause);
                    break;
                }
            }
            return(true);
        }
Beispiel #2
0
 public ApplyAnnotationVisitor(LetIdentifierAnnotation annotation, string identifier)
 {
     this.annotation = annotation;
     this.identifier = identifier;
 }