internal IEnumerable <SparqlResult> Having(IEnumerable <SparqlResult> resultsGroups, RdfQuery11Translator q)
        {
            //   IEnumerable<SparqlResult> resultsGroups = enumerable as SparqlResult[] ?? enumerable.ToArray();
            //if (!resultsGroups.Any()) return resultsGroups; //todo

            return(this.Aggregate(resultsGroups, (result, testExpr) =>
            {
                switch (testExpr.AggregateLevel)
                {
                case SparqlExpression.VariableDependenceGroupLevel.Const:
                    return (bool)testExpr.Const.Content ? result : Enumerable.Empty <SparqlResult>();

                case SparqlExpression.VariableDependenceGroupLevel.UndependableFunc:
                    return testExpr.Test(null) ? result : Enumerable.Empty <SparqlResult>();

                case SparqlExpression.VariableDependenceGroupLevel.SimpleVariable:
                    return result.Where(testExpr.Test);

                case SparqlExpression.VariableDependenceGroupLevel.Group:
                    var sparqlGroupOfResults = new SparqlGroupOfResults(q)
                    {
                        Group = result.Select(sparqlResult => sparqlResult.Clone()).ToArray()
                    };
                    return testExpr.Test(sparqlGroupOfResults) ? sparqlGroupOfResults.Group : Enumerable.Empty <SparqlResult>();

                case SparqlExpression.VariableDependenceGroupLevel.GroupOfGroups:
                default:
                    throw new Exception("requested grouping");
                }
            }));
        }
        internal IEnumerable <SparqlGroupOfResults> Having4CollectionGroups(
            IEnumerable <SparqlGroupOfResults> resultsGroups, RdfQuery11Translator q)
        {
            return(this.Aggregate(resultsGroups, (result, testExpr) =>
            {
                switch (testExpr.AggregateLevel)
                {
                case SparqlExpression.VariableDependenceGroupLevel.Const:
                    if ((bool)testExpr.Const.Content)
                    {
                        return result;
                    }
                    else
                    {
                        return Enumerable.Empty <SparqlGroupOfResults>();
                    }

                case SparqlExpression.VariableDependenceGroupLevel.UndependableFunc:
                    if ((bool)testExpr.Operator(null))
                    {
                        return result;
                    }
                    else
                    {
                        return Enumerable.Empty <SparqlGroupOfResults>();
                    }

                case SparqlExpression.VariableDependenceGroupLevel.SimpleVariable:
                case SparqlExpression.VariableDependenceGroupLevel.Group:
                    return result.Where(testExpr.Test);

                case SparqlExpression.VariableDependenceGroupLevel.GroupOfGroups:

                    var group = new SparqlGroupOfResults(q)
                    {
                        Group = result.Select(sparqlResult => sparqlResult.Clone()).ToArray()
                    };
                    return testExpr.Test(group) ? group.Group.Cast <SparqlGroupOfResults>() : Enumerable.Empty <SparqlGroupOfResults>();

                default:
                    throw new ArgumentOutOfRangeException();
                }

                return resultsGroups;
            }));
        }
        public IEnumerable <SparqlResult> Run4Grouped(IEnumerable <SparqlResult> variableBindings)
        {
            switch (sparqlExpression.AggregateLevel)
            {
            case SparqlExpression.VariableDependenceGroupLevel.Const:
                return(variableBindings.Select(
                           variableBinding =>
                {
                    variableBinding.Add(variableNode, sparqlExpression.Const);
                    return variableBinding;
                }));

                break;

            case SparqlExpression.VariableDependenceGroupLevel.UndependableFunc:
            case SparqlExpression.VariableDependenceGroupLevel.SimpleVariable:

            case SparqlExpression.VariableDependenceGroupLevel.Group:
                return(RunAddVar(variableBindings));

                break;

            case SparqlExpression.VariableDependenceGroupLevel.GroupOfGroups:
                var arr           = variableBindings as SparqlResult[] ?? variableBindings.Select(result => result.Clone()).ToArray();
                var groupOfGroups = new SparqlGroupOfResults(q)
                {
                    Group = arr
                };
                return(arr.Select(variableBinding =>
                {
                    variableBinding.Add(variableNode, sparqlExpression.TypedOperator(groupOfGroups));
                    return variableBinding;
                }));

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }