Enumeration Extension Methods

The Encodian population engine provides features to perform common manipulations on a collection (Iterable). These features mimic the extension methods provided by IEnumerable<T>, thus,  you can group, sort, and perform other sequential data manipulations in template expressions in a common way.

In the below examples, the term persons refers to the data collection (array, list, or enumerable) that the method operates on. It’s simply a placeholder for any collection you might have in your JSON data.

 

Extension Method Example
All(Predicate)

Check if all elements in a collection satisfy a specified condition.

persons.All(p => p.Age < 50)
Any()
Persons.Any()
Any(Predicate)

Checks if any element in a collection satisfies a specified condition. It returns true if at least one item matches the condition, or false if none do. 

persons.Any(p => p.Name == "John Smith")

Example:

<<if [persons.Any(p => p.Age > 30)]>>There is someone older than 30.<</if>>
Average(Selector)

Calculate the mean value of a numeric property across all items in. a collection.

persons.Average(p => p.Age)
Concat(IEnumerable)
persons.Concat(otherPersons)
Contains(Object)

Check if a key value or collection contains an item that matches a specified condition. 

persons.Contains(otherPersons.First())

-------------

Example template syntax

<<foreach [person in persons]>>
<<if [person.Name.Contains(“John”)]>>
<<[Name]>>
<</if>><</foreach>>

Example JSON:

{
"persons": [
{ "Name": "John Smith"},
{ "Name": "Alice Williams"},
{ "Name": "Bob Taylor"}
]
}

Word document result:

John Smith

Expanding on the example above you can change the syntax slightly to find everything that doesn't match the condition. 

<<foreach [person in persons]>>
<<if [!(person.Name.Contains(“John”))]>>
<<[Name]>>
<</if>><</foreach>>

Word document result:

Alice Williams
Bob Taylor
Count()
persons.Count()
Count(Predicate)
persons.Count(p => p.Age > 30)
Distinct()
persons.Distinct()
First()
persons.First()
First(Predicate)
persons.First(p => p.Age > 30)
FirstOrDefault()
persons.FirstOrDefault()
FirstOrDefault(Predicate)
persons.FirstOrDefault(p => p.Age > 30)
GroupBy(Selector)

Group items in a collection based on a specified property or key. 

persons.GroupBy(p => p.Age)

or

persons.GroupBy( p => new { Age = p.Age, Count = p.Children.Count() })

-------------

Example template syntax:

<<foreach [group in persons.GroupBy(p => p.City)]>>
City: <<[group.Key]>>
<<foreach [person in group]>>
Name: <<[person.Name]>>
<</foreach>>
<</foreach>>

Example JSON:

{
"persons": [
{ "Name": "John", "City": "London" },
{ "Name": "Alice", "City": "New York" },
{ "Name": "Bob", "City": "London" }
]
}

Word document result:

City: London
Name: John
Name: Bob

City: New York
Name: Alice
Last()
persons.Last()
Last(Predicate)
persons.Last(p => p.Age > 100)
LastOrDefault()
persons.LastOrDefault()
LastOrDefault(Predicate)
persons.LastOrDefault(p => p.Age > 100)
Max(ComparableSelector)
persons.Max(p => p.Age)
Min(ComparableSelector)
persons.Min(p => p.Age)
OrderBy(ComparableSelector)
persons.OrderBy(p => p.Age)

Or

persons.OrderBy(p => p.Age).ThenByDescending(p => p.Name)
OrderByDescending(ComparableSelector)
persons.OrderByDescending(p => p.Age)

Or

persons.OrderByDescending(p => p.Age).ThenByDescending(p => p.Name)
Select(Selector)
persons.Select(p => p.Name)
SelectMany(EnumerationSelector)
persons.SelectMany(p => p.Children)
Single()
persons.Single()
Single(Predicate)
persons.Single(p => p.Name == "John Smith")
SingleOrDefault()
persons.SingleOrDefault()
SingleOrDefault(Predicate)
persons.SingleOrDefault(p => p.Name == "John Smith")
Skip(int)
persons.Skip(10)
SkipWhile(Predicate)
persons.SkipWhile(p => p.Age < 21)
Sum(Selector)
persons.Sum(p => p.Children.Count())
Take(int)
persons.Take(5)
TakeWhile(Predicate)
persons.TakeWhile(p => p.Age < 50)
Union(IEnumerable)
persons.Union(otherPersons)
Where(Predicate)
persons.Where(p => p.Age > 18)

 

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk