Enumeration Extension Methods

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Assembly MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-assembly -- dnx GroupDocs.Assembly.Mcp --yes
Folders, licensing and more →

LINQ Reporting Engine enables you to perform common manipulations on a sequential data through the engine’s built-in extension methods for IEnumerable. These extension methods mimic some extension methods of IEnumerable<T> providing the same signatures and behavior features. Thus you can group, sort and perform other sequential data manipulations in template expressions in a familiar 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 IComparable.
  • EnumerationSelector stands for Selector returning IEnumerable.
  • 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.

public class Person
{
    public String Name { get { ... } }
    public int Age { get { ... } }
    public IEnumerable<Person> Children { get { ... } }
    ...
}
Extension MethodExamples and Notes
All(Predicate)
persons.All(p => p.Age < 50)
Any()
persons.Any()
Any(Predicate)
persons.Any(p => p.Name == "John Smith")
Average(Selector)
persons.Average(p => p.Age)
The input selector must return a value of any type that has predefined or user-defined addition and division operators.
Concat(IEnumerable)
persons.Concat(otherPersons)
An implicit reference conversion must exist between types of items of concatenated enumerations.
Contains(Object)
persons.Contains(otherPersons.First())
Count()
persons.Count()
Count(Predicate)
persons.Count(p => p.Age > 30)
Distinct()
persons.Distinct()
Items are compared as described in Comparing Keys of GroupBy and 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)
persons.GroupBy(p => p.Age)
Or
persons.GroupBy(    p => new { Age = p.Age, Count = p.Children.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 property. You can treat a group itself as an enumeration of items that the group contains. Keys are compared as described in Comparing Keys of GroupBy and Distinct.
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)
Or
persons.OrderBy(p => p.Age).ThenByDescending(p => p.Name).ThenBy(p => p.Children.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)
persons.OrderByDescending(p => p.Age)
Or
persons.OrderByDescending(p => p.Age).ThenByDescending(p => p.Name)
Or
persons.OrderByDescending(p => p.Age).ThenByDescending(p => p.Name).ThenBy(p => p.Children.Count())
See the previous note.
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())
The input selector must return a value of any type that has a predefined or user-defined addition operator.
Take(int)
persons.Take(5)
TakeWhile(Predicate)
persons.TakeWhile(p => p.Age < 50)
Union(IEnumerable)
persons.Union(otherPersons)
An implicit reference conversion must exist between types of items of united enumerations.
Where(Predicate)
persons.Where(p => p.Age > 18)

Predicates over Nullable Boolean Values

Note
This feature is supported by version 26.11 or greater.

Boolean values of JSON, XML and CSV data, and bool? members of your own classes, are nullable because the value may be missing. Such values can be returned by a Predicate directly, without == true. A missing value counts as false.

Suppose we have JSON data like:

[
  { "Title": "Order beans", "Done": true },
  { "Title": "Clean grinder", "Done": false },
  { "Title": "Call supplier", "Done": null }
]

If we use the template like following:

Done: <<[tasks.Count(t => t.Done)]>> of <<[tasks.Count()]>>
Not done: <<foreach [t in tasks.Where(t => t.Done != true)]>><<[t.Title]>>; <</foreach>>
All done: <<[tasks.All(t => t.Done)]>>

The result will be like:

Done: 1 of 3
Not done: Clean grinder; Call supplier; 
All done: False
Warning
A negation of a missing value is missing as well, so tasks.Where(t => !t.Done) returns the tasks with false only. To include missing values, compare with true: t.Done != true.

Comparing Keys of GroupBy and Distinct

Note
This feature is supported by version 26.11 or greater.

GroupBy and Distinct compare keys and items depending on where they come from:

  • Nested objects of JSON and XML data are compared by value, including their own nested objects: {"Name":"A"} and {"Name":"A"} make one group. Objects of JSON data with different $id values are different objects, even if their values are equal.
  • Rows of a DataSet of your own are compared by identity: orders of the same customer row make one group, while two customer rows with equal values stay two groups.
  • Objects of your own classes are compared as .NET does it, using their Equals and GetHashCode methods.

To group by a value rather than by an object, use a simple key, such as GroupBy(o => o.Customer.Name).

For example, orders 1 and 3 below hold equal customer objects:

{ "Orders": [
  { "Number": 1, "Paid": true,  "Customer": { "Name": "Quayside Bistro" } },
  { "Number": 2, "Paid": false, "Customer": { "Name": "Harbor Lane Provisions" } },
  { "Number": 3, "Paid": null,  "Customer": { "Name": "Quayside Bistro" } } ] }

The following template:

<<foreach [g in ds.Orders.GroupBy(o => o.Customer)]>><<[g.Key.Name]>>: <<[g.Count()]>> order(s), paid: <<[g.Count(o => o.Paid)]>>
<<foreach [o in g]>>  #<<[o.Number]>> <<[o.Paid ? "paid" : "due"]>>
<</foreach>><</foreach>>
JsonDataLoadOptions options = new JsonDataLoadOptions();
options.AlwaysGenerateRootObject = true;

new DocumentAssembler().AssembleDocument("orders.txt", "report.txt",
    new DataSourceInfo(new JsonDataSource("orders.json", options), "ds"));

gives one group per customer:

Quayside Bistro: 2 order(s), paid: 1
  #1 paid
  #3 due
Harbor Lane Provisions: 1 order(s), paid: 0
  #2 due

Example: UseFileDataLikeTypedObjects.cs.