---
title: "Appendix A. Enumeration Extension Methods"
---


LINQ Reporting Engine enables you to perform common manipulations on a sequential data through the engine's built-in extension methods for `Iterable`. These extension methods mimic some extension methods of [IEnumerable&lt;T&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1?view=net-6.0) providing the same signatures and behavior features. Thus, you can group, sort, and perform other sequential data manipulations in template expressions in a common way.

The following table describes these built-in extension methods. The following notation conventions are used within the table:

- `Selector` stands for a lambda function returning a value and taking an enumeration item as its single argument. See “Using Lambda Functions” for more information.
- `ComparableSelector` stands for `Selector` returning [Comparable](https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html).
- `EnumerationSelector` stands for `Selector` returning `Iterable`.
- `Predicate` stands for `Selector` returning a `Boolean` value.

Examples in the following table are given using `persons` and `otherPersons`, enumerations of instances of the `Person` class that is defined as follows.

```csharp
public class Person
{
	public String getName() { ... }
	public int getAge() { ... }
	public Iterable<Person> getChildren() { ... }
	...
}
```

| Extension Method | Examples and Notes |
| :- | :- |
| `all(Predicate)` | ```csharp
 persons.all(p => p.getAge() < 50)``` |
| `any()` | ```csharp
 persons.any()``` |
| `any(Predicate)` | ```csharp
 persons.any(p => p.getName() == "John Smith")``` |
| `average(Selector)` | ```csharp
 persons.average(p => p.getAge())```The input selector must return a value of any type that has predefined addition and division operators. |
| `concat(Iterable)` | ```csharp
 persons.concat(otherPersons)```An implicit reference conversion must exist between types of items of concatenated enumerations. |
| `contains(Object)` | ```csharp
 persons.contains(otherPersons.first())``` |
| `count()` | ```csharp
 persons.count()``` |
| `count(Predicate)` | ```csharp
 persons.count(p => p.getAge() > 30)``` |
| `distinct()` | ```csharp
 persons.distinct()``` |
| `elementAt(int)` | ```csharp
 persons.elementAt(3)``` |
| `elementAtOrDefault(int)` | ```csharp
 persons.elementAtOrDefault(5)``` |
| `first()` | ```csharp
 persons.first()``` |
| `first(Predicate)` | ```csharp
 persons.first(p => p.getAge() > 30)``` |
| `firstOrDefault()` | ```csharp
 persons.firstOrDefault()``` |
| `firstOrDefault(Predicate)` | ```csharp
 persons.firstOrDefault(p => p.getAge() > 30)``` |
| `groupBy(Selector)` | ```csharp
 persons.groupBy(p => p.getAge())```Or ```csharp
 persons.groupBy(p => new { age = p.getAge(), count = p.getChildren().count() })```This method returns an enumeration of group objects. Each group has a unique key defined by the input selector and contains items of the source enumeration associated with this key. You can access the key of a group instance using the `key` field. You can treat a group itself as an enumeration of items that the group contains. |
| `last()` | ```csharp
 persons.last()``` |
| `last(Predicate)` | ```csharp
 persons.last(p => p.getAge() > 100)``` |
| `lastOrDefault()` | ```csharp
 persons.lastOrDefault()``` |
| `lastOrDefault(Predicate)` | ```csharp
 persons.lastOrDefault(p => p.getAge() > 100)``` |
| `max(ComparableSelector)` | ```csharp
 persons.max(p => p.getAge())``` |
| `min(ComparableSelector)` | ```csharp
 persons.min(p => p.getAge())``` |
| `orderBy(ComparableSelector)` | ```csharp
 persons.orderBy(p => p.getAge())```Or ```csharp
 persons.orderBy(p => p.getAge()).thenByDescending(p => p.getName())```Or ```csharp
 persons.orderBy(p => p.getAge()).thenByDescending(p => p.getName()).thenBy(p => p.getChildren().count())```This method returns an enumeration ordered by a single key. To specify additional ordering keys, you can use the following extension methods of an ordered enumeration:- thenBy(ComparableSelector)- thenByDescending(ComparableSelector) |
| `orderByDescending(ComparableSelector)` | ```csharp
 persons.orderByDescending(p => p.getAge())```Or ```csharp
 persons.orderByDescending(p => p.getAge()).thenByDescending(p => p.getName())```Or ```csharp
 persons.orderByDescending(p => p.getAge()).thenByDescending(p => p.getName())    .thenBy(p => p.getChildren().count())```See the previous note. |
| `select(Selector)` | ```csharppersons.select(p => p.getName())``` |
| `selectMany(EnumerationSelector)` | ```csharppersons.selectMany(p => p.getChildren())``` |
| `single()` | ```csharp
 persons.single()``` |
| `single(Predicate)` | ```csharp
 persons.single(p => p.getName() == "John Smith")``` |
| `singleOrDefault()` | ```csharp
 persons.singleOrDefault()``` |
| `singleOrDefault(Predicate)` | ```csharp
 persons.singleOrDefault(p => p.getName() == "John Smith")``` |
| `skip(int)` | ```csharp
 persons.skip(10)``` |
| `skipWhile(Predicate)` | ```csharp
 persons.skipWhile(p => p.getAge() < 21)``` |
| `sum(Selector)` | ```csharp
 persons.sum(p => p.getChildren().count())```The input selector must return a value of any type that has a predefined addition operator. |
| `take(int)` | ```csharp
 persons.take(5)``` |
| `takeWhile(Predicate)` | ```csharp
 persons.takeWhile(p => p.getAge() < 50)``` |
| `union(Iterable)` | ```csharp
 persons.union(otherPersons)```An implicit reference conversion must exist between types of items of united enumerations. |
| `where(Predicate)` | ```csharp
 persons.where(p => p.getAge() > 18)``` |

------ 

## FAQ

1. **Q:** How can I obtain the index of the current item inside a `foreach` loop in a template?  
   **A:** Call the `indexOf()` extension method on the iteration variable. For example, inside `<<foreach [item in items]>>` write `<<[item.indexOf()]>>` to output the zero‑based position of the current element (`numberOf()` gives the one‑based position).

2. **Q:** Is there a way to access the previous element while iterating over a collection?  
   **A:** The engine does not expose a direct `previous()` method. A common workaround is to use `skip(1)` together with `first()` on a sliced collection, e.g., `persons.skip(1).first()` gives the element after the first one, which you can compare with the current index.

3. **Q:** How do I group items by a calculated key, such as age, and then iterate over each group?  
   **A:** Use the `groupBy` extension method. Example: `persons.groupBy(p => p.getAge())` returns a collection of groups. Inside the template you can iterate: `<<foreach [group in persons.groupBy(p => p.getAge())]>> Age: <<[group.key]>> Count: <<[group.count()]>> <</foreach>>`.

4. **Q:** What extension methods should I use to sort a collection by multiple fields?  
   **A:** First apply `orderBy` (or `orderByDescending`) with the primary key, then chain `thenBy` or `thenByDescending` for secondary keys. Example: `persons.orderBy(p => p.getAge()).thenByDescending(p => p.getName()).thenBy(p => p.getChildren().count())`.

5. **Q:** How can I check whether any element in a collection satisfies a condition?  
   **A:** Use the `any(Predicate)` method. Example: `persons.any(p => p.getAge() > 65)` returns `true` if at least one person is older than 65, otherwise `false`.