Count
The Count function is a numeric aggregate function that returns the number of elements in a collection that satisfy an optional criteria. It can count all elements in a collection or only those that meet a specified condition.
Function Name: Count
Return Type: Number (Double)
Syntax
The Count function can be called with the following syntax:
Syntax 1 - Count All Elements:
Count(collection)
Syntax 2 - Count with Criteria:
Count(collection, criteria)
Named Parameter Syntax:
Count(collection -> <collection_expression>, criteria -> <lambda_expression>)
Parameters
Parameter |
Type |
Required |
Description |
|---|---|---|---|
|
Collection |
Yes |
The collection of elements to count. Can be an array, query result, or any collection expression. |
|
Lambda |
No |
A lambda expression that defines a condition for counting. Takes one parameter representing the current element and returns a boolean. If omitted, counts all elements in the collection. |
Return Value
Type:
Number(double precision)- Returns:
The total number of elements in the collection if no criteria is specified
The number of elements that satisfy the criteria if a criteria lambda is provided
0if the collection is empty0if no elements satisfy the criteria
Evaluation Rules
The Count function follows these evaluation rules:
No Criteria: When criteria is omitted, returns the total count of elements without iteration
With Criteria: When criteria is provided, evaluates each element and counts those where the criteria returns
trueEmpty Collections: Returns
0for empty collectionsNull/Undefined Handling: Null or undefined values are passed to the criteria lambda; the lambda determines whether to count them
Boolean Criteria: When provided, the criteria lambda must return a boolean value
Complete Iteration: Unlike
AnyorAll,Countalways processes all elements to get an accurate countOrder Independence: The order of elements doesn’t affect the count
Use Cases
The Count function is useful for:
Size Metrics: Determining the number of elements in filtered collections
Statistical Analysis: Counting occurrences of specific conditions
Validation: Verifying expected quantities of items
Business Metrics: Calculating KPIs like “number of active customers”
Data Quality: Counting missing, invalid, or exceptional data points
Conditional Reporting: Generating counts based on complex conditions
Examples
{
"Comment_Count_1": "Retrieve count of all employees older than 40 with salary greater than 100000.",
"Count_1": "$value(Count(Companies.Select(c => c.Employees.Where(e => e.Age >= 40 && e.Salary > 100000))))",
"Comment_Count_2": "Another way to Retrieve count of all employees older than 40 with salary greater than 100000.",
"Count_2": "$value(Count(Companies.Select(c => c.Employees).Where(e => e.Age >= 40), e => e.Salary > 100000))",
"Comment_Count_3": "Demo of using named parameters to make the intent clear.",
"Count_3": "$value(Count(collection -> Companies.Select(c => c.Employees), criteria -> e => e.Age >= 40 && e.Salary > 100000))"
}