Any
The Any function is a boolean aggregate function that determines whether at least one element in a collection satisfies a specified condition, or whether a collection contains any elements at all. It returns true if any element meets the criteria (or if the collection is non-empty when no criteria is provided), or false otherwise.
Function Name: Any
Return Type: Boolean
Syntax
The Any function can be called with the following syntax:
Syntax 1 - Check for Non-Empty Collection:
Any(collection)
Syntax 2 - Check with Criteria:
Any(collection, criteria)
Named Parameter Syntax:
Any(collection -> <collection_expression>, criteria -> <lambda_expression>)
Parameters
Parameter |
Type |
Required |
Description |
|---|---|---|---|
|
Collection |
Yes |
The collection of elements to evaluate. Can be an array, query result, or any collection expression. |
|
Lambda |
No |
A lambda expression that defines the condition to check. Takes one parameter representing the current element and returns a boolean. If omitted, the function simply checks if the collection is non-empty. |
Return Value
Type:
Boolean- Returns:
trueif at least one element in the collection satisfies the criteriatrueif the collection is non-empty and no criteria is specifiedfalseif no elements satisfy the criteriafalseif the collection is empty
Evaluation Rules
The Any function follows these evaluation rules:
Short-Circuit Evaluation: Stops evaluating as soon as any element satisfies the criteria and returns
trueEmpty Collections: Returns
falsefor empty collectionsNo Criteria: When criteria is omitted, returns
truefor any non-empty collectionNull/Undefined Handling: Null or undefined values are passed to the criteria lambda; the lambda determines how to handle them
Early Exit: Only needs one matching element to return
trueBoolean Criteria: When provided, the criteria lambda must return a boolean value
Order of Evaluation: Elements are evaluated in the order they appear in the collection
Use Cases
The Any function is useful for:
Existence Checks: Verifying that at least one item meets certain criteria
Filtering Detection: Determining if filtering will yield any results before performing expensive operations
Validation: Checking for the presence of specific values or conditions
Business Logic: Implementing rules like “at least one item must be approved”
Data Quality: Detecting presence of invalid or problematic data
Collection Testing: Simply checking if a collection contains any elements
Examples
{
"Comment_Any_1": "Check if salary of any employee older than 40 is greater than 144000.",
"Any_1": "$value(Any(Companies.Select(c => c.Employees).Where(e => e.Age >= 40), e => e.Salary > 144000))",
"Comment_Any_2": "Another way to check check if salary of any employee older than 40 is greater than 144000.",
"Any_2": "$value(Any(Companies.Select(c => c.Employees), e => e.Age < 40 || e.Salary > 144000))",
"Comment_Any_3": "Check if collection has any item. Optional 'criteria' parameter is not used.",
"Any_3": "$value(Any(Companies.Select(c => c.Employees)))",
"Comment_Any_4": "Demo of using named parameters to make the intent clear.",
"Any_4": "$value(Any(collection -> Companies.Select(c => c.Employees), criteria -> e => e.Age < 40 || e.Salary > 144000))"
}