/// <summary>Visits a where clause.</summary> /// <param name="whereClause">Where clause to be visited.</param> /// <param name="queryModel">Query model containing given from clause.</param> /// <param name="index">Index of the where clause in the query model.</param> public override void VisitWhereClause(WhereClause whereClause, Remotion.Linq.QueryModel queryModel, int index) { _visitor.ConstantFromClause = _auxFromComponent; _visitor.Visit(whereClause.Predicate); QueryComponent queryComponent = _visitor.RetrieveComponent(); IQueryComponentNavigator queryComponentNavigator = queryComponent.GetQueryComponentNavigator(); if (queryComponentNavigator != null) { queryComponentNavigator.ReplaceComponent(Identifier.Current, _subject); } if (queryComponent is QueryElement) { if ((!(queryComponent is EntityConstrain)) && (!_query.Elements.Contains((QueryElement)queryComponent))) { _query.Elements.Add((QueryElement)queryComponent); } } else if (!_query.FindAllComponents <Filter>().Any(item => item.Expression == queryComponent)) { Filter filter = new Filter((IExpression)queryComponent); StrongEntityAccessor targetEntityAccessor = null; if ((_subject != _mainFromComponent.About) && (queryComponentNavigator.Contains(_subject))) { targetEntityAccessor = (from entityAccessor in _query.FindAllComponents <StrongEntityAccessor>() from constrain in entityAccessor.FindAllComponents <EntityConstrain>() where (constrain.GetType() == typeof(EntityConstrain)) && (_subject.Equals(constrain.Value)) select entityAccessor).FirstOrDefault(); } if (targetEntityAccessor == null) { IEnumerable <StrongEntityAccessor> targetEntityAccessorExression = _query.Elements.OfType <StrongEntityAccessor>(); if ((_query.IsSubQuery) || (whereClause.Predicate is Remotion.Linq.Clauses.Expressions.SubQueryExpression)) { targetEntityAccessorExression = targetEntityAccessorExression.Except(new StrongEntityAccessor[] { _query.Elements.OfType <StrongEntityAccessor>().LastOrDefault() }); } targetEntityAccessor = targetEntityAccessorExression.LastOrDefault() ?? _mainFromComponent; } if ((!targetEntityAccessor.Elements.Contains(queryComponent)) && (!targetEntityAccessor.Elements.Contains(filter))) { targetEntityAccessor.Elements.Add(filter); } } _auxFromComponent = null; base.VisitWhereClause(whereClause, queryModel, index); }
/// <summary>Visits a select clause.</summary> /// <param name="selectClause">Select clause to be visited.</param> /// <param name="queryModel">Query model containing given select clause.</param> public override void VisitSelectClause(SelectClause selectClause, Remotion.Linq.QueryModel queryModel) { if (queryModel.ResultOperators.Count == 0) { throw new InvalidOperationException("Must have an evaluating expression for sub-queries, i.e. 'Count' or 'Contains'."); } string currentItemNameOverride = _visitor.ItemNameOverride; QuerySourceReferenceExpression selector = (QuerySourceReferenceExpression)selectClause.Selector; if (selector.ReferencedQuerySource is MainFromClause) { MainFromClause mainFrom = (MainFromClause)selector.ReferencedQuerySource; if (mainFrom.FromExpression is System.Linq.Expressions.MemberExpression) { _fromExpression = (System.Linq.Expressions.MemberExpression)mainFrom.FromExpression; } } _visitor.ItemNameOverride = ((QuerySourceReferenceExpression)selectClause.Selector).ReferencedQuerySource.ItemName; queryModel.MainFromClause.Accept(this, queryModel); VisitBodyClauses(queryModel.BodyClauses, queryModel); VisitResultOperators(queryModel.ResultOperators, queryModel); IQueryComponentNavigator resultNavigator = _result.GetQueryComponentNavigator(); if ((_from != null) && (_from is IExpression)) { if (_mainFromComponent == null) { _mainFromComponent = (IExpression)_from; } else { resultNavigator.ReplaceComponent(Identifier.Current, _from); } } if (_mainFromComponent != null) { if (_bodies.Count == 0) { resultNavigator.AddComponent(_mainFromComponent); } else { foreach (QueryComponent queryComponent in _bodies) { if (queryComponent is IExpression) { IExpression expression = (IExpression)queryComponent; Identifier currentIdentifier = null; IQueryComponentNavigator queryComponentNavigator = expression.GetQueryComponentNavigator(); if (queryComponentNavigator != null) { currentIdentifier = (_mainFromComponent is Identifier ? (Identifier)_mainFromComponent : _visitor.Query.FindAllComponents <Identifier>().Where(item => _visitor.Query.RetrieveIdentifier(item.Name) == _visitor.ItemNameOverride).FirstOrDefault()) ?? _visitor.Query.Subject; queryComponentNavigator.ReplaceComponent(Identifier.Current, currentIdentifier); } resultNavigator.AddComponent(new Filter(expression)); } } } } else { throw new InvalidOperationException(System.String.Format("Cannot add value of type '{0}' as an method call argument.", _from.GetType().FullName)); } _visitor.ItemNameOverride = currentItemNameOverride; _visitor.ConstantFromClause = null; }