Esempio n. 1
0
        /// <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>()));
        }
Esempio n. 2
0
        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);
        }