private ISqlFragment buildWhereForContains(ArrayField field)
            {
                if (_contains is ConstantExpression c)
                {
                    var flattened           = new FlattenerStatement(field, _parent._session, _parent._statement);
                    var idSelectorStatement = new ContainsIdSelectorStatement(flattened, _parent._session, c);
                    return(new WhereInSubQuery(idSelectorStatement.ExportName));
                }

                throw new NotSupportedException();
            }
            private ISqlFragment buildWhereForAny(ArrayField field)
            {
                if (_wheres.Any())
                {
                    var flattened = new FlattenerStatement(field, _parent._session, _parent._statement);


                    var itemType      = _expression.QueryModel.MainFromClause.ItemType;
                    var elementFields =
                        _parent._session.Options.ChildTypeMappingFor(itemType);

                    var idSelectorStatement = new IdSelectorStatement(_parent._session, elementFields, flattened);
                    idSelectorStatement.WhereClauses.AddRange(_wheres);
                    idSelectorStatement.CompileLocal(_parent._session);

                    return(new WhereInSubQuery(idSelectorStatement.ExportName));
                }

                return(new CollectionIsNotEmpty(field));
            }
            public CountComparisonStatement BuildCountComparisonStatement()
            {
                if (Usage != SubQueryUsage.Count)
                {
                    throw new BadLinqExpressionException("Invalid comparison");
                }

                var field     = findArrayField();
                var flattened = new FlattenerStatement(field, _parent._session, _parent._statement);

                var elementFields =
                    _parent._session.Options.ChildTypeMappingFor(field.ElementType);
                var statement = new CountComparisonStatement(_parent._session, field.ElementType, elementFields, flattened);

                if (_wheres.Any())
                {
                    statement.WhereClauses.AddRange(_wheres);
                    statement.CompileLocal(_parent._session);
                }

                return(statement);
            }