public void VisitResultOperator_WithCast () { var constantExpression = Expression.Constant (new Cook()); _sqlStatementBuilder.SelectProjection = constantExpression; var castResultOperator = new CastResultOperator (typeof (Chef)); _handler.HandleResultOperator (castResultOperator, _sqlStatementBuilder, _generator, _stageMock, _context); Assert.That (_sqlStatementBuilder.SelectProjection, Is.TypeOf (typeof (UnaryExpression))); Assert.That (_sqlStatementBuilder.SelectProjection.NodeType, Is.EqualTo(ExpressionType.Convert)); Assert.That (((UnaryExpression) _sqlStatementBuilder.SelectProjection).Operand, Is.SameAs(constantExpression)); Assert.That (_sqlStatementBuilder.DataInfo, Is.TypeOf (typeof (StreamedSequenceInfo))); Assert.That (((StreamedSequenceInfo) _sqlStatementBuilder.DataInfo).DataType, Is.EqualTo (typeof (IQueryable<>).MakeGenericType (typeof (Chef)))); }
private static Expression HandleCast( EntityQueryModelVisitor entityQueryModelVisitor, CastResultOperator castResultOperator) { var resultItemTypeInfo = entityQueryModelVisitor.Expression.Type .GetSequenceType().GetTypeInfo(); return(castResultOperator.CastItemType.GetTypeInfo() .IsAssignableFrom(resultItemTypeInfo) ? entityQueryModelVisitor.Expression : Expression.Call( entityQueryModelVisitor.LinqOperatorProvider .Cast.MakeGenericMethod(castResultOperator.CastItemType), entityQueryModelVisitor.Expression)); }
public void SetUp() { _resultOperator = new CastResultOperator(typeof(Chef)); }
public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index) { resultOptionType = resultOptionType ?? queryModel.SelectClause.Selector.Type; if (resultOperator is CastResultOperator) { CastResultOperator castResultOperator = (CastResultOperator)resultOperator; Type castType = castResultOperator.CastItemType; MethodInfo cast = GetMethod("Cast", expression.Type).MakeGenericMethod(castType); resultOptionType = castType; expression = Expression.Call(cast, expression); return; } if (resultOperator is AllResultOperator) { AllResultOperator allResultOperator = (AllResultOperator)resultOperator; ParameterExpression parameterExpression = Expression.Parameter(selectorType, "p"); Expression predicateUpdate = UpdateExpressionVisitor.Update(allResultOperator.Predicate, new[] { parameterExpression }, dbContext, queryContext); Expression allLamda = Expression.Lambda(predicateUpdate, parameterExpression); MethodInfo all = GetMethod("All", expression.Type, 1).MakeGenericMethod(selectorType); expression = Expression.Call(all, new[] { expression, allLamda }); return; } if (resultOperator is FirstResultOperator) { FirstResultOperator firstResultOperator = (FirstResultOperator)resultOperator; if (firstResultOperator.ReturnDefaultWhenEmpty) { MethodInfo firstOrDefault = GetMethod("FirstOrDefault", expression.Type, 0).MakeGenericMethod(selectorType); expression = Expression.Call(firstOrDefault, new[] { expression }); } else { MethodInfo first = GetMethod("First", expression.Type, 0).MakeGenericMethod(selectorType); expression = Expression.Call(first, new[] { expression }); } return; } if (resultOperator is SingleResultOperator) { SingleResultOperator singleResultOperator = (SingleResultOperator)resultOperator; if (singleResultOperator.ReturnDefaultWhenEmpty) { MethodInfo firstOrDefault = GetMethod("SingleOrDefault", expression.Type, 0).MakeGenericMethod(selectorType); expression = Expression.Call(firstOrDefault, new[] { expression }); } else { MethodInfo first = GetMethod("Single", expression.Type, 0).MakeGenericMethod(selectorType); expression = Expression.Call(first, new[] { expression }); } return; } if (resultOperator is AnyResultOperator) { MethodInfo any = GetMethod("Any", expression.Type).MakeGenericMethod(selectorType); expression = Expression.Call(any, new[] { expression }); return; } if (resultOperator is TrackingResultOperator) { MethodInfo asNoTracking = typeof(EntityFrameworkQueryableExtensions).GetMethod("AsNoTracking").MakeGenericMethod(selectorType); expression = Expression.Call(asNoTracking, new[] { expression }); return; } if (resultOperator is CountResultOperator) { MethodInfo count = GetMethod("Count", expression.Type).MakeGenericMethod(selectorType); expression = Expression.Call(count, new[] { expression }); return; } if (resultOperator is AverageResultOperator) { MethodInfo average = GetAgregateMethod("Average", expression.Type, selectorType); expression = Expression.Call(average, new[] { expression }); return; } if (resultOperator is ContainsResultOperator) { ContainsResultOperator containsResultOperator = (ContainsResultOperator)resultOperator; Expression valExp; object value; ParameterExpression paramExp = containsResultOperator.Item as ParameterExpression; if (paramExp != null && queryContext.ParameterValues.TryGetValue(paramExp.Name, out value)) { valExp = Expression.Constant(value); } else { valExp = containsResultOperator.Item; } if (containsResultOperator.Item is SubQueryExpression) { SubQueryExpression subQueryExpression = (SubQueryExpression)containsResultOperator.Item; QueryModelVisitor queryModelVisitor = new QueryModelVisitor(dbContext, queryContext); queryModelVisitor.VisitQueryModel(subQueryExpression.QueryModel); valExp = queryModelVisitor.expression; } MethodInfo contains = GetMethod("Contains", expression.Type, 1).MakeGenericMethod(selectorType); expression = Expression.Call(contains, new[] { expression, valExp }); return; } if (resultOperator is DefaultIfEmptyResultOperator) { DefaultIfEmptyResultOperator defaultIfEmptyResultOperator = (DefaultIfEmptyResultOperator)resultOperator; MethodInfo defaultIfEmpty; if (defaultIfEmptyResultOperator.OptionalDefaultValue != null) { defaultIfEmpty = GetMethod("DefaultIfEmpty", expression.Type, 1).MakeGenericMethod(selectorType); expression = Expression.Call(defaultIfEmpty, new[] { expression, defaultIfEmptyResultOperator.OptionalDefaultValue }); } defaultIfEmpty = GetMethod("DefaultIfEmpty", expression.Type).MakeGenericMethod(selectorType); expression = Expression.Call(defaultIfEmpty, expression); return; } if (resultOperator is DistinctResultOperator) { MethodInfo distinct = GetMethod("Distinct", expression.Type).MakeGenericMethod(selectorType); expression = Expression.Call(distinct, expression); return; } if (resultOperator is GroupResultOperator) { GroupResultOperator groupResultOperator = (GroupResultOperator)resultOperator; Type keySelectorType = GetTypeParameter(groupResultOperator.KeySelector); ParameterExpression keyExpressionParam = Expression.Parameter(keySelectorType, "p"); Expression keyExpression = UpdateExpressionVisitor.Update(groupResultOperator.KeySelector, new[] { keyExpressionParam }, dbContext, queryContext); LambdaExpression keyLambdaExpression = Expression.Lambda(keyExpression, keyExpressionParam); Type elementSelectorType = GetTypeParameter(groupResultOperator.ElementSelector); ParameterExpression ElementExpressionParam = Expression.Parameter(elementSelectorType, "p"); Expression ElementExpression = UpdateExpressionVisitor.Update(groupResultOperator.ElementSelector, new[] { ElementExpressionParam }, dbContext, queryContext); LambdaExpression ElementLambdaExpression = Expression.Lambda(ElementExpression, ElementExpressionParam); Type tSource = queryModel.MainFromClause.ItemType; Type tKey = keyExpression.Type; Type tElement = ElementExpression.Type; Type tResult = queryModel.ResultTypeOverride; MethodInfo groupBy = GetMethods("GroupBy", expression.Type, 2).Where(p => p.GetParameters()[2].Name == "elementSelector").Single(). MakeGenericMethod(tSource, tKey, tElement); expression = Expression.Call(groupBy, new[] { expression, keyLambdaExpression, ElementLambdaExpression }); return; } if (resultOperator is LastResultOperator) { LastResultOperator lastResultOperator = (LastResultOperator)resultOperator; if (lastResultOperator.ReturnDefaultWhenEmpty) { MethodInfo lastOrDefault = GetMethod("LastOrDefault", expression.Type, 0).MakeGenericMethod(selectorType); expression = Expression.Call(lastOrDefault, new[] { expression }); } else { MethodInfo last = GetMethod("Last", expression.Type, 0).MakeGenericMethod(selectorType); expression = Expression.Call(last, new[] { expression }); } return; } if (resultOperator is LongCountResultOperator) { MethodInfo longCount = GetMethod("LongCount", expression.Type).MakeGenericMethod(selectorType); expression = Expression.Call(longCount, new[] { expression }); return; } if (resultOperator is MaxResultOperator) { MethodInfo max = GetMethod("Max", expression.Type).MakeGenericMethod(selectorType); expression = Expression.Call(max, expression); return; } if (resultOperator is MinResultOperator) { MethodInfo min = GetMethod("Min", expression.Type).MakeGenericMethod(selectorType); expression = Expression.Call(min, expression); return; } if (resultOperator is SumResultOperator) { MethodInfo sum = GetAgregateMethod("Sum", expression.Type, selectorType); expression = Expression.Call(sum, expression); return; } if (resultOperator is SkipResultOperator) { SkipResultOperator skipResultOperator = (SkipResultOperator)resultOperator; Expression expVal; object value; ParameterExpression paramExp = skipResultOperator.Count as ParameterExpression; if (paramExp != null && queryContext.ParameterValues.TryGetValue(paramExp.Name, out value)) { expVal = Expression.Constant(value); } else { expVal = skipResultOperator.Count; } MethodInfo skip = GetMethod("Skip", expression.Type, 1).MakeGenericMethod(selectorType); expression = Expression.Call(skip, new[] { expression, expVal }); return; } if (resultOperator is TakeResultOperator) { TakeResultOperator takeResultOperator = (TakeResultOperator)resultOperator; Expression expVal; object value; ParameterExpression paramExp = takeResultOperator.Count as ParameterExpression; if (paramExp != null && queryContext.ParameterValues.TryGetValue(paramExp.Name, out value)) { expVal = Expression.Constant(value); } else { expVal = takeResultOperator.Count; } MethodInfo take = GetMethod("Take", expression.Type, 1).MakeGenericMethod(selectorType); expression = Expression.Call(take, new[] { expression, expVal }); return; } if (resultOperator is IncludeResultOperator) { IncludeResultOperator includeResultOperator = (IncludeResultOperator)resultOperator; Expression includeExpression = includeResultOperator.NavigationPropertyPath; Type paramExpressionType = null; ParameterExpression parameterExpression = null; if (includeExpression is MemberExpression) { MemberExpression memberExpression = (MemberExpression)includeExpression; paramExpressionType = memberExpression.Expression.Type; parameterExpression = Expression.Parameter(paramExpressionType, "p"); includeExpression = Expression.Property(parameterExpression, memberExpression.Member.Name); } else { paramExpressionType = GetTypeParameter(includeExpression); parameterExpression = Expression.Parameter(paramExpressionType, "p"); } Expression updateOuterExpression = UpdateExpressionVisitor.Update(includeExpression, new[] { parameterExpression }, dbContext, queryContext); LambdaExpression lambdaIncludeExpression = Expression.Lambda(updateOuterExpression, parameterExpression); MethodInfo include = typeof(EntityFrameworkQueryableExtensions).GetMethods().First(m => m.Name == "Include").MakeGenericMethod(selectorType, updateOuterExpression.Type); expression = Expression.Call(include, new[] { expression, lambdaIncludeExpression }); if (includeResultOperator.ChainedNavigationProperties != null) { foreach (PropertyInfo propertyInfo in includeResultOperator.ChainedNavigationProperties) { Type propertyType = propertyInfo.PropertyType; Type argument = expression.Type.GetGenericArguments().Last(); MethodInfo thenInclude; Type realType; if (typeof(IEnumerable).IsAssignableFrom(argument)) { realType = argument.GetGenericArguments().First(); thenInclude = ThenIncludeCollection.MakeGenericMethod(includeResultOperator.QuerySource.ItemType, realType, propertyType); } else { realType = argument; thenInclude = ThenIncludeProperty.MakeGenericMethod(includeResultOperator.QuerySource.ItemType, realType, propertyType); } ParameterExpression parameterThenIncludeExpression = Expression.Parameter(realType, "p"); MemberExpression property = Expression.Property(parameterThenIncludeExpression, propertyInfo); LambdaExpression lambdaThenIncludeExpression = Expression.Lambda(property, parameterThenIncludeExpression); expression = Expression.Call(thenInclude, new[] { expression, lambdaThenIncludeExpression }); } } return; } if (resultOperator is OfTypeResultOperator) { OfTypeResultOperator ofTypeResultOperator = (OfTypeResultOperator)resultOperator; selectorType = ofTypeResultOperator.SearchedItemType; var miOfType = GetMethod("OfType", expression.Type).MakeGenericMethod(ofTypeResultOperator.SearchedItemType); expression = Expression.Call(miOfType, new[] { expression }); return; } throw new NotSupportedException(); }
private static Expression HandleCast( EntityQueryModelVisitor entityQueryModelVisitor, CastResultOperator castResultOperator) { var resultItemTypeInfo = entityQueryModelVisitor.Expression.Type .GetSequenceType().GetTypeInfo(); if (castResultOperator.CastItemType.GetTypeInfo() .IsAssignableFrom(resultItemTypeInfo)) { return entityQueryModelVisitor.Expression; } return Expression.Call( entityQueryModelVisitor.LinqOperatorProvider .Cast.MakeGenericMethod(castResultOperator.CastItemType), entityQueryModelVisitor.Expression); }
public void SetUp () { _resultOperator = new CastResultOperator (typeof (Chef)); }
private static Expression HandleCast( EntityQueryModelVisitor entityQueryModelVisitor, CastResultOperator castResultOperator) { return Expression.Call( entityQueryModelVisitor.LinqOperatorProvider .Cast.MakeGenericMethod(castResultOperator.CastItemType), entityQueryModelVisitor.Expression); }