internal static LinqLambdaExpression CreateLambda(QueryExpression source, IEnumerable <KeyValuePair <QueryProperty, QueryConstantExpression> > keys) { ExceptionUtilities.CheckArgumentNotNull(source, "source"); ExceptionUtilities.CheckCollectionNotEmpty(keys, "keys"); QueryType parameterType; if (source.ExpressionType.IsUnresolved) { parameterType = QueryType.Unresolved; } else { var queryCollectionType = source.ExpressionType as QueryCollectionType; ExceptionUtilities.CheckObjectNotNull(queryCollectionType, "Source expression type was not a collection. Type was: {0}", source.ExpressionType); parameterType = queryCollectionType.ElementType; } var parameter = LinqBuilder.Parameter(ParameterName, parameterType); // specifically using the overload of .Property which takes a type because it may have already been resolved and we don't want to throw that away var predicate = keys.Select(k => parameter.Property(k.Key.Name, k.Key.PropertyType).EqualTo(k.Value)).ToList(); QueryExpression body = predicate[0]; if (predicate.Count > 1) { for (int i = 1; i < predicate.Count; i++) { body = CommonQueryBuilder.And(body, predicate[i]); } } return(LinqBuilder.Lambda(body, parameter)); }
/// <summary> /// Ensures that the given parameter has a name and creates a new instance if not. /// </summary> /// <param name="expression">The expression.</param> /// <returns>Parameter with a name.</returns> /// <remarks>Subsequent calls to this method with the same expression /// will return the same instance of parameter.</remarks> public LinqParameterExpression EnsureParameterHasName(LinqParameterExpression expression) { LinqParameterExpression parameterWithName; if (!this.parameterMappings.TryGetValue(expression, out parameterWithName)) { if (expression.Name == null) { parameterWithName = LinqBuilder.Parameter(this.parameterNameGenerator.GenerateIdentifier("p"), expression.ExpressionType); } else { parameterWithName = expression; } this.parameterMappings.Add(expression, parameterWithName); } return(parameterWithName); }
private LinqLambdaExpression ResolveLambdaTypes(LinqLambdaExpression lambda, params QueryCollectionType[] parameterCollectionTypes) { ExceptionUtilities.Assert(lambda.Parameters.Count == parameterCollectionTypes.Length, "Lambda expression parameter numbers must match the length of parameterCollectionTypes."); for (int i = 0; i < lambda.Parameters.Count; i++) { var lambdaParameter = lambda.Parameters[i]; this.parameterMappings.Add(lambdaParameter, LinqBuilder.Parameter(lambdaParameter.Name, parameterCollectionTypes[i].ElementType)); } try { return((LinqLambdaExpression)this.ResolveTypes(lambda)); } finally { foreach (var p in lambda.Parameters) { this.parameterMappings.Remove(p); } } }