Пример #1
0
        internal static TypeUsage ValidateGroupBy(
            IEnumerable <KeyValuePair <string, DbExpression> > keys,
            IEnumerable <KeyValuePair <string, DbAggregate> > aggregates,
            out DbExpressionList validKeys,
            out ReadOnlyCollection <DbAggregate> validAggregates)
        {
            List <KeyValuePair <string, TypeUsage> > columns = new List <KeyValuePair <string, TypeUsage> >();
            HashSet <string> keyNames = new HashSet <string>();
            EnumerableValidator <KeyValuePair <string, DbExpression>, DbExpression, DbExpressionList> validator1 = ArgumentValidation.CreateValidator <KeyValuePair <string, DbExpression>, DbExpression, DbExpressionList>(keys, nameof(keys), (Func <KeyValuePair <string, DbExpression>, int, DbExpression>)((keyInfo, index) =>
            {
                ArgumentValidation.CheckNamed <DbExpression>(keyInfo, nameof(keys), index);
                if (!TypeHelpers.IsValidGroupKeyType(keyInfo.Value.ResultType))
                {
                    throw new ArgumentException(Strings.Cqt_GroupBy_KeyNotEqualityComparable((object)keyInfo.Key));
                }
                keyNames.Add(keyInfo.Key);
                columns.Add(new KeyValuePair <string, TypeUsage>(keyInfo.Key, keyInfo.Value.ResultType));
                return(keyInfo.Value);
            }), (Func <List <DbExpression>, DbExpressionList>)(expList => new DbExpressionList((IList <DbExpression>)expList)));

            validator1.AllowEmpty = true;
            validator1.GetName    = (Func <KeyValuePair <string, DbExpression>, int, string>)((keyInfo, idx) => keyInfo.Key);
            validKeys             = validator1.Validate();
            bool hasGroupAggregate = false;
            EnumerableValidator <KeyValuePair <string, DbAggregate>, DbAggregate, ReadOnlyCollection <DbAggregate> > validator2 = ArgumentValidation.CreateValidator <KeyValuePair <string, DbAggregate>, DbAggregate, ReadOnlyCollection <DbAggregate> >(aggregates, nameof(aggregates), (Func <KeyValuePair <string, DbAggregate>, int, DbAggregate>)((aggInfo, idx) =>
            {
                ArgumentValidation.CheckNamed <DbAggregate>(aggInfo, nameof(aggregates), idx);
                if (keyNames.Contains(aggInfo.Key))
                {
                    throw new ArgumentException(Strings.Cqt_GroupBy_AggregateColumnExistsAsGroupColumn((object)aggInfo.Key));
                }
                if (aggInfo.Value is DbGroupAggregate)
                {
                    if (hasGroupAggregate)
                    {
                        throw new ArgumentException(Strings.Cqt_GroupBy_MoreThanOneGroupAggregate);
                    }
                    hasGroupAggregate = true;
                }
                columns.Add(new KeyValuePair <string, TypeUsage>(aggInfo.Key, aggInfo.Value.ResultType));
                return(aggInfo.Value);
            }), (Func <List <DbAggregate>, ReadOnlyCollection <DbAggregate> >)(aggList => ArgumentValidation.NewReadOnlyCollection <DbAggregate>((IList <DbAggregate>)aggList)));

            validator2.AllowEmpty = true;
            validator2.GetName    = (Func <KeyValuePair <string, DbAggregate>, int, string>)((aggInfo, idx) => aggInfo.Key);
            validAggregates       = validator2.Validate();
            if (validKeys.Count == 0 && validAggregates.Count == 0)
            {
                throw new ArgumentException(Strings.Cqt_GroupBy_AtLeastOneKeyOrAggregate);
            }
            return(ArgumentValidation.CreateCollectionOfRowResultType(columns));
        }
Пример #2
0
        internal static TypeUsage ValidateNewRow(
            IEnumerable <KeyValuePair <string, DbExpression> > columnValues,
            out DbExpressionList validElements)
        {
            List <KeyValuePair <string, TypeUsage> > columnTypes = new List <KeyValuePair <string, TypeUsage> >();
            EnumerableValidator <KeyValuePair <string, DbExpression>, DbExpression, DbExpressionList> validator = ArgumentValidation.CreateValidator <KeyValuePair <string, DbExpression>, DbExpression, DbExpressionList>(columnValues, nameof(columnValues), (Func <KeyValuePair <string, DbExpression>, int, DbExpression>)((columnValue, idx) =>
            {
                ArgumentValidation.CheckNamed <DbExpression>(columnValue, nameof(columnValues), idx);
                columnTypes.Add(new KeyValuePair <string, TypeUsage>(columnValue.Key, columnValue.Value.ResultType));
                return(columnValue.Value);
            }), (Func <List <DbExpression>, DbExpressionList>)(expList => new DbExpressionList((IList <DbExpression>)expList)));

            validator.GetName = (Func <KeyValuePair <string, DbExpression>, int, string>)((columnValue, idx) => columnValue.Key);
            validElements     = validator.Validate();
            return(ArgumentValidation.CreateResultType((EdmType)TypeHelpers.CreateRowType((IEnumerable <KeyValuePair <string, TypeUsage> >)columnTypes)));
        }