private bool IsGroupByAggregateSubQuery(QueryModel queryModel) { if (queryModel.MainFromClause.FromExpression.Type.IsGrouping() && queryModel.BodyClauses.Count == 0 && queryModel.ResultOperators.Count == 1 && _aggregateResultOperators.Contains(queryModel.ResultOperators[0].GetType()) && _queryModelStack.Count > 0 && !_queryModelStack.Peek().BodyClauses.OfType <IQuerySource>().Any()) { var groupResultOperator = (GroupResultOperator) ((SubQueryExpression) ((FromClauseBase)queryModel.MainFromClause.FromExpression.TryGetReferencedQuerySource()) .FromExpression) .QueryModel.ResultOperators.Last(); MemberAccessBindingExpressionVisitor.GetPropertyPath( queryModel.SelectClause.Selector, _queryModelVisitor.QueryCompilationContext, out var qsre); if (qsre != null || queryModel.SelectClause.Selector.RemoveConvert() is ConstantExpression || groupResultOperator.ElementSelector.NodeType == ExpressionType.New || groupResultOperator.ElementSelector.NodeType == ExpressionType.MemberInit) { return(true); } } return(false); }
private Expression RewriteNullEquality(ExpressionType nodeType, Expression nonNullExpression) { var properties = MemberAccessBindingExpressionVisitor .GetPropertyPath(nonNullExpression, _queryCompilationContext, out var qsre); if (properties.Count > 0 && properties[properties.Count - 1] is INavigation lastNavigation && lastNavigation.IsCollection()) { // collection navigation is only null if its parent entity is null (null propagation thru navigation) // it is probable that user wanted to see if the collection is (not) empty // log warning suggesting to use Any() instead. _queryCompilationContext.Logger .PossibleUnintendedCollectionNavigationNullComparisonWarning(properties); return(Visit( Expression.MakeBinary( nodeType, UnwrapLastNavigation(nonNullExpression), Expression.Constant(null)))); } if (IsInvalidSubQueryExpression(nonNullExpression)) { return(null); } var entityType = _model.FindEntityType(nonNullExpression.Type) ?? GetEntityType(properties, qsre); if (entityType == null) { return(null); } var keyProperties = entityType.FindPrimaryKey().Properties; var nullCount = keyProperties.Count; Expression keyAccessExpression; // Skipping composite key with subquery since it requires to copy subquery // which would cause same subquery to be visited twice if (nullCount > 1 && nonNullExpression.RemoveConvert() is SubQueryExpression) { return(null); } if (properties.Count > 0 && properties[properties.Count - 1] is INavigation navigation2 && navigation2.IsDependentToPrincipal()) { keyAccessExpression = CreateKeyAccessExpression( UnwrapLastNavigation(nonNullExpression), navigation2.ForeignKey.Properties, nullComparison: false); nullCount = navigation2.ForeignKey.Properties.Count; }