Skip to content

SingleOrDefault

Dennis Corvers edited this page Oct 23, 2026 · 1 revision

Enumerable:singleOrDefault

Returns the only value matching an optional predicate, or a default when no value matches.

Syntax

value = values:singleOrDefault(predicate, defaultValue)

Parameters

Parameter Type Description
predicate function(value, sourceKey) (optional) Returns true for a matching value.
defaultValue any (optional) Value returned when there are no matches.

Returns

The only matching value, or defaultValue if there are no matches.

Remarks

Raises an error when more than one value matches. Pass nil as the predicate to supply only a default.

Example

local assignedRole = LuaQ.from({
    { player = "Ada", assigned = true },
    { player = "Bryn", assigned = false },
})
    :singleOrDefault(function(entry) return entry.assigned end,
        { player = "Unassigned" })
-- assignedRole.player is "Ada"

See also

Single · FirstOrDefault · Functions

Clone this wiki locally