/// <summary> /// Orders the collection by applying given ordering. /// </summary> /// <param name="ordering">The ordering to use.</param> /// <returns>Ordered collection.</returns> /// <remarks>This overload allows for more intellisense-friendly way of specifying ordering.</remarks> public QueryCollectionValue OrderBy(QueryOrdering ordering) { ExceptionUtilities.CheckArgumentNotNull(ordering, "ordering"); ExceptionUtilities.CheckCollectionNotEmpty(ordering.Selectors, "ordering.Selectors"); if (this.IsNull) { return(this); } IEnumerable <QueryValue> orderedElements = ordering.Apply(this.Elements.Cast <object>()).Cast <QueryValue>(); QueryError currentError = QueryError.Combine(this.EvaluationError, QueryError.GetErrorFromValues(orderedElements)); if (currentError != null) { return((QueryCollectionValue)this.Type.CreateErrorValue(currentError)); } // if this collection contains duplicate order by key selectors if (HasNondeterministicOrdering(ordering, orderedElements)) { return(new QueryCollectionValue(this.Type, this.EvaluationStrategy, null, orderedElements, false)); } else { return(new QueryCollectionValue(this.Type, this.EvaluationStrategy, null, orderedElements, true)); } }
/// <summary> /// Combines elements from both collections into one without eliminating duplicates. /// </summary> /// <param name="collection">The input collection for the union all operation</param> /// <returns>Combined elements from both collections.</returns> public QueryCollectionValue UnionAll(QueryCollectionValue collection) { List <QueryValue> resultValues = new List <QueryValue>(); resultValues.AddRange(this.Elements); resultValues.AddRange(collection.Elements); return(new QueryCollectionValue(this.Type, this.EvaluationStrategy, QueryError.GetErrorFromValues(resultValues), resultValues)); }
/// <summary> /// Creates the collection value with the specified elements. /// </summary> /// <param name="elementType">The type of each collection element.</param> /// <param name="elements">The elements.</param> /// <returns> /// Strongly-typed collection with the specified values. /// </returns> public static QueryCollectionValue Create(QueryType elementType, params QueryValue[] elements) { ExceptionUtilities.CheckArgumentNotNull(elementType, "elementType"); return(new QueryCollectionValue( elementType.CreateCollectionType(), elementType.EvaluationStrategy, QueryError.GetErrorFromValues(elements), elements)); }
/// <summary> /// Applies a projection operator which selects a value from each collection element. /// </summary> /// <param name="selector">The selector.</param> /// <returns> /// Collection of values containing results of the selector applied to all elements of the collection. /// </returns> public QueryCollectionValue Select(Func <QueryValue, QueryValue> selector) { ExceptionUtilities.CheckArgumentNotNull(selector, "selector"); IEnumerable <QueryValue> result; QueryCollectionType resultType; this.ComputeSelectResult(selector, out result, out resultType); return(new QueryCollectionValue(resultType, this.EvaluationStrategy, QueryError.GetErrorFromValues(result), result, this.IsSorted)); }
/// <summary> /// Creates the collection value with the specified elements. /// </summary> /// <param name="elementType">The type of each collection element.</param> /// <param name="elements">The elements.</param> /// <param name="isSorted">Indicating if the collection is sorted.</param> /// <returns>Strongly-typed collection with the specified values.</returns> public static QueryCollectionValue Create(QueryType elementType, IEnumerable <QueryValue> elements, bool isSorted) { ExceptionUtilities.CheckArgumentNotNull(elementType, "elementType"); return(new QueryCollectionValue( elementType.CreateCollectionType(), elementType.EvaluationStrategy, QueryError.GetErrorFromValues(elements), elements, isSorted)); }
private QueryStructuralValue CreateGroupingValue(QueryValue key, QueryCollectionValue elements) { QueryGroupingType groupingType = new QueryGroupingType(key.Type, elements.Type.ElementType, key.Type.EvaluationStrategy); var error = QueryError.GetErrorFromValues(elements.Elements.Concat(new[] { key })); var result = new QueryStructuralValue(groupingType, false, error, groupingType.EvaluationStrategy); result.SetValue("Key", key); result.SetValue("Elements", elements); return(result); }
/// <summary> /// Applies a projection operator which selects a value from each collection element. /// </summary> /// <param name="selector">The selector.</param> /// <param name="resultType">Type of the result</param> /// <returns> /// Collection of values containing results of the selector applied to all elements of the collection. /// </returns> public QueryCollectionValue Select(Func <QueryValue, QueryValue> selector, QueryCollectionType resultType) { ExceptionUtilities.CheckArgumentNotNull(selector, "selector"); IEnumerable <QueryValue> result = null; if (!this.IsNull) { result = this.Elements.Cast <QueryValue>().Select(e => selector(e)).ToList(); } return(new QueryCollectionValue(resultType, this.EvaluationStrategy, QueryError.GetErrorFromValues(result), result, this.IsSorted)); }
/// <summary> /// Gets distinct elements from the collection. /// </summary> /// <returns>Distinct elements of the collection.</returns> public QueryCollectionValue Distinct() { List <QueryValue> distinctValues = new List <QueryValue>(); foreach (var element in this.Elements) { if (!distinctValues.Any(v => this.ValuesAreEqual(v, element))) { distinctValues.Add(element); } } return(new QueryCollectionValue(this.Type, this.EvaluationStrategy, QueryError.GetErrorFromValues(distinctValues), distinctValues)); }
/// <summary> /// Applies the Take operation on the given collection. /// </summary> /// <param name="takeCount">How many elements to take.</param> /// <returns>Collection with the applied Take operation.</returns> public QueryCollectionValue Take(QueryScalarValue takeCount) { ExceptionUtilities.CheckArgumentNotNull(takeCount, "takeCount"); QueryError currentError = QueryError.Combine(this.EvaluationError, takeCount.EvaluationError); if (currentError != null) { return((QueryCollectionValue)this.Type.CreateErrorValue(currentError)); } IEnumerable <QueryValue> result = null; if (!this.IsNull) { result = this.Elements.Take((int)takeCount.Value).ToList(); } return(new QueryCollectionValue(this.Type, this.Type.EvaluationStrategy, QueryError.GetErrorFromValues(result), result, this.IsSorted)); }
/// <summary> /// Applies the Skip operation on the given collection. /// </summary> /// <param name="skipCount">How many elements to skip.</param> /// <returns>Collection with the applied Skip operation.</returns> public QueryCollectionValue Skip(QueryScalarValue skipCount) { ExceptionUtilities.CheckArgumentNotNull(skipCount, "skipCount"); QueryError currentError = QueryError.Combine(this.EvaluationError, skipCount.EvaluationError); if (currentError != null) { return((QueryCollectionValue)this.Type.CreateErrorValue(currentError)); } IEnumerable <QueryValue> result = null; if (!this.IsNull) { // perhaps this must be changed - moved to EvaluationStrategy result = this.Elements.Skip((int)skipCount.Value); } return(new QueryCollectionValue(this.Type, this.Type.EvaluationStrategy, QueryError.GetErrorFromValues(result), result, this.IsSorted)); }
/// <summary> /// Groups elements using key computed from the key selector. /// </summary> /// <param name="keySelector">Key selector lambda.</param> /// <returns>Elements grouped based on the provided key.</returns> public QueryCollectionValue GroupBy(Func <QueryValue, QueryValue> keySelector) { if (this.IsNull) { return(new QueryCollectionValue(this.Type.ElementType.CreateCollectionType(), this.EvaluationStrategy, this.EvaluationError, null)); } var keys = this.Elements.Select(keySelector).ToList(); var keyCollection = new QueryCollectionValue(this.Type.ElementType.CreateCollectionType(), this.EvaluationStrategy, this.EvaluationError, keys); var distinctKeys = keyCollection.Distinct().Elements; var elementType = this.Type.ElementType; var groupings = distinctKeys.Select(key => { if (key.IsNull) { var matchingNullElements = this.Elements.Where(element => keySelector(element).IsNull); return(this.CreateGroupingValue(key, QueryCollectionValue.Create(elementType, matchingNullElements.ToArray()))); } else { return(this.CreateGroupingValue(key, QueryCollectionValue.Create(elementType, this.Elements.Where(element => this.ValuesAreEqual(keySelector(element), key)).ToArray()))); } }); var keyType = keySelector(elementType.NullValue).Type; var groupingType = new QueryGroupingType(keyType, elementType, this.EvaluationStrategy); return(new QueryCollectionValue(groupingType.CreateCollectionType(), this.EvaluationStrategy, QueryError.GetErrorFromValues(groupings), groupings.Cast <QueryValue>())); }
/// <summary> /// Returns the first collection concatenanted with the second collection. /// </summary> /// <param name="collection">The collection to concatenate with.</param> /// <returns>The concatenated result.</returns> public QueryCollectionValue Concat(QueryCollectionValue collection) { var resultValues = this.Elements.Concat(collection.Elements); return(new QueryCollectionValue(this.Type, this.EvaluationStrategy, QueryError.GetErrorFromValues(resultValues), resultValues)); }
/// <summary> /// Returns the first collection without the common elements from both input collections. /// </summary> /// <param name="collection">The input collection for the except operation</param> /// <returns>The firt collection with commmon elements removed.</returns> public QueryCollectionValue Except(QueryCollectionValue collection) { var resultValues = new List <QueryValue>(); foreach (QueryValue element in this.Elements) { if (!collection.Elements.Any(v => this.ValuesAreEqual(v, element))) { resultValues.Add(element); } } // except removes duplicates from the result, so applying distinct at the end var exceptResult = new QueryCollectionValue(this.Type, this.EvaluationStrategy, QueryError.GetErrorFromValues(resultValues), resultValues); var result = exceptResult.Distinct(); return(result); }
/// <summary> /// Creates a collection with values the specified values. /// </summary> /// <param name="elements">The collection elements. If null is passed, the collection will be a null /// collection.</param> /// <returns>Newly created collection value.</returns> public virtual QueryCollectionValue CreateCollectionWithValues(IEnumerable <QueryValue> elements) { return(new QueryCollectionValue(this, this.EvaluationStrategy, QueryError.GetErrorFromValues(elements), elements)); }