Use Simple Aggregates (Sum, Count, Average)
Leave feedback
Overview
GroupDocs.Assembly for .NET supports LINQ-based aggregate functions that allow you to calculate sums, counts, averages, and other aggregate values directly in templates. These functions work with collections and enable you to display totals, summaries, and statistical information in your documents.
The main aggregate functions are:
Sum() - calculate sum of numeric values
Count() - count number of items
Average() - calculate average of numeric values
Min() - find minimum value
Max() - find maximum value
Here are the steps to use aggregates:
Create a template with aggregate expressions
Use LINQ aggregate methods on collections
Reference aggregate results in your template
Assemble the document with your data
Note
Aggregate functions use LINQ syntax and work with any collection type, including lists, arrays, DataTables, and other enumerable collections.
usingGroupDocs.Assembly;usingSystem.Collections.Generic;publicstaticvoidCalculateMinMax(){List<Sale>sales=newList<Sale>{newSale{Amount=1000.00m},newSale{Amount=1500.00m},newSale{Amount=800.00m}};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("AggregatesTemplate.docx","AggregatesOutput.docx",newDataSourceInfo(sales,"sales"));Console.WriteLine("Min and max calculated successfully.");}
Template:
Minimum Sale: <<[sales.Min(s => s.Amount)]:"C">>
Maximum Sale: <<[sales.Max(s => s.Amount)]:"C">>
<<foreach [in orders]>>
Order #<<[OrderId]>>
Items:
<<foreach [in Items]>>
<<[ProductName]>> - <<[Price]:"C">> x <<[Quantity]>>
<</foreach>>
Order Total: <<[Items.Sum(i => i.Price * i.Quantity)]:"C">>
<</foreach>>
Warning
Aggregate functions require collections. Ensure your data source is a collection type (List, Array, DataTable, etc.) when using aggregates.
Advanced Usage Topics
To learn more about grouping, filtering with aggregates, custom aggregate functions, and performance optimization, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: