AvaloniaUI/Avalonia · error · ArgumentException

Need more than one query to OR.

Error message

Need more than one query to OR.

What it means

Thrown by the OrQuery constructor (container-query OR group) when the supplied query list has zero or one elements. A logical OR is only meaningful with at least two operands, so fewer is treated as a programming error rather than silently returning the single query.

Source

Thrown at src/Avalonia.Base/Styling/OrQuery.cs:31

    internal sealed class OrQuery : StyleQuery
    {
        private readonly IReadOnlyList<StyleQuery> _queries;
        private string? _queryString;

        /// <summary>
        /// Initializes a new instance of the <see cref="OrQuery"/> class.
        /// </summary>
        /// <param name="queries">The querys to OR.</param>
        public OrQuery(IReadOnlyList<StyleQuery> queries)
        {
            if (queries is null)
            {
                throw new ArgumentNullException(nameof(queries));
            }

            if (queries.Count <= 1)
            {
                throw new ArgumentException("Need more than one query to OR.");
            }

            _queries = queries;
        }

        /// <inheritdoc/>
        internal override bool IsCombinator => false;

        /// <inheritdoc/>
        public override string ToString(ContainerQuery? owner)
        {
            if (_queryString == null)
            {
                _queryString = string.Join(", ", _queries.Select(x => x.ToString(owner)));
            }

            return _queryString;
        }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Ensure at least two StyleQuery instances are passed to the OrQuery constructor.
  2. If the list is dynamic, short-circuit: if only one query remains, use it directly instead of wrapping in OrQuery; if zero, skip.
  3. Validate the count before constructing.

Example fix

// before
var orq = new OrQuery(filteredQueries); // filteredQueries may have <=1 element

// after
StyleQuery result = filteredQueries.Count switch
{
    0 => throw new InvalidOperationException("no queries"),
    1 => filteredQueries[0],
    _ => new OrQuery(filteredQueries)
};
Defensive patterns

Strategy: validation

Validate before calling

if (queries is null || queries.Count < 2)
    throw new ArgumentException("OrQuery requires at least two queries.", nameof(queries));
var orq = new OrQuery(queries);

Type guard

static bool CanOr(IReadOnlyList<StyleQuery> q) => q is { Count: >= 2 };

Prevention

When it happens

Trigger: Constructing an OrQuery directly with an empty or single-element list, or producing a container query OR group via a factory/builder that ends up with fewer than two queries (e.g. filtering removed queries).

Common situations: Programmatically building container queries and OR-ing a list that was dynamically filtered down to one or zero elements; passing `Array.Empty<StyleQuery>()` by mistake.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/0a6a91d46196261b. Report an issue: GitHub.