Use Simple Aggregates (Sum, Count, Average)
Leave feedback
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 valuesCount()- count number of itemsAverage()- calculate average of numeric valuesMin()- find minimum valueMax()- 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
NoteAggregate functions use LINQ syntax and work with any collection type, including lists, arrays, DataTables, and other enumerable collections.
Calculate the sum of values in a collection:
using GroupDocs.Assembly;
using System.Collections.Generic;
public class OrderItem
{
public string ProductName { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
public static void CalculateSum()
{
List<OrderItem> items = new List<OrderItem>
{
new OrderItem { ProductName = "Laptop", Price = 999.99m, Quantity = 1 },
new OrderItem { ProductName = "Mouse", Price = 29.99m, Quantity = 2 },
new OrderItem { ProductName = "Keyboard", Price = 79.99m, Quantity = 1 }
};
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("AggregatesTemplate.docx", "AggregatesOutput.docx",
new DataSourceInfo(items, "items"));
Console.WriteLine("Sum calculated successfully.");
}
In your template (AggregatesTemplate.docx):
Items:
<<foreach [in items]>>
<<[ProductName]>> - <<[Price]:"C">> x <<[Quantity]>>
<</foreach>>
Total: <<[items.Sum(i => i.Price * i.Quantity)]:"C">>
Count the number of items in a collection:
using GroupDocs.Assembly;
using System.Collections.Generic;
public static void CalculateCount()
{
List<string> products = new List<string>
{
"Laptop",
"Mouse",
"Keyboard",
"Monitor"
};
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("AggregatesTemplate.docx", "AggregatesOutput.docx",
new DataSourceInfo(products, "products"));
Console.WriteLine("Count calculated successfully.");
}
Template:
Products:
<<foreach [p in products]>>- <<[p]>>
<</foreach>>
Total Products: <<[products.Count()]>>
The products are strings, so the data band names its iteration variable (p) and outputs it with <<[p]>>.
Calculate the average of numeric values:
using GroupDocs.Assembly;
using System.Collections.Generic;
public class Sale
{
public decimal Amount { get; set; }
}
public static void CalculateAverage()
{
List<Sale> sales = new List<Sale>
{
new Sale { Amount = 1000.00m },
new Sale { Amount = 1500.00m },
new Sale { Amount = 800.00m },
new Sale { Amount = 1200.00m }
};
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("AggregatesTemplate.docx", "AggregatesOutput.docx",
new DataSourceInfo(sales, "sales"));
Console.WriteLine("Average calculated successfully.");
}
Template:
Sales:
<<foreach [in sales]>>
<<[Amount]:"C">>
<</foreach>>
Average Sale: <<[sales.Average(s => s.Amount)]:"C">>
Find minimum and maximum values:
using GroupDocs.Assembly;
using System.Collections.Generic;
public static void CalculateMinMax()
{
List<Sale> sales = new List<Sale>
{
new Sale { Amount = 1000.00m },
new Sale { Amount = 1500.00m },
new Sale { Amount = 800.00m }
};
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("AggregatesTemplate.docx", "AggregatesOutput.docx",
new DataSourceInfo(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">>
Use multiple aggregates in the same template:
using GroupDocs.Assembly;
using System.Collections.Generic;
public static void MultipleAggregates()
{
List<OrderItem> items = new List<OrderItem>
{
new OrderItem { ProductName = "Laptop", Price = 999.99m, Quantity = 1 },
new OrderItem { ProductName = "Mouse", Price = 29.99m, Quantity = 2 },
new OrderItem { ProductName = "Keyboard", Price = 79.99m, Quantity = 1 }
};
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("AggregatesTemplate.docx", "AggregatesOutput.docx",
new DataSourceInfo(items, "items"));
Console.WriteLine("Multiple aggregates calculated successfully.");
}
Template:
Items:
<<foreach [in items]>>
<<[ProductName]>> - <<[Price]:"C">> x <<[Quantity]>>
<</foreach>>
Summary:
Total Items: <<[items.Count()]>>
Total Quantity: <<[items.Sum(i => i.Quantity)]>>
Total Amount: <<[items.Sum(i => i.Price * i.Quantity)]:"C">>
Average Price: <<[items.Average(i => i.Price)]:"C">>
Use aggregates with filtered data:
using GroupDocs.Assembly;
using System.Collections.Generic;
public static void AggregatesWithConditions()
{
List<OrderItem> items = new List<OrderItem>
{
new OrderItem { ProductName = "Laptop", Price = 999.99m, Quantity = 1 },
new OrderItem { ProductName = "Mouse", Price = 29.99m, Quantity = 2 },
new OrderItem { ProductName = "Keyboard", Price = 79.99m, Quantity = 1 }
};
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("AggregatesTemplate.docx", "AggregatesOutput.docx",
new DataSourceInfo(items, "items"));
Console.WriteLine("Conditional aggregates calculated successfully.");
}
Template:
High-value items (Price > 100):
<<foreach [in items.Where(i => i.Price > 100)]>>
<<[ProductName]>> - <<[Price]:"C">>
<</foreach>>
Total High-value: <<[items.Where(i => i.Price > 100).Sum(i => i.Price)]:"C">>
Calculate aggregates for detail records within master records:
using GroupDocs.Assembly;
using System.Collections.Generic;
public class Order
{
public int OrderId { get; set; }
public List<OrderItem> Items { get; set; }
}
public static void AggregatesInMasterDetail()
{
List<Order> orders = new List<Order>
{
new Order
{
OrderId = 1001,
Items = new List<OrderItem>
{
new OrderItem { ProductName = "Laptop", Price = 999.99m, Quantity = 1 },
new OrderItem { ProductName = "Mouse", Price = 29.99m, Quantity = 2 }
}
}
};
DocumentAssembler assembler = new DocumentAssembler();
assembler.AssembleDocument("AggregatesTemplate.docx", "AggregatesOutput.docx",
new DataSourceInfo(orders, "orders"));
Console.WriteLine("Master-detail aggregates calculated successfully.");
}
Template:
<<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>>
WarningAggregate functions require collections. Ensure your data source is a collection type (List, Array, DataTable, etc.) when using aggregates.
To learn more about grouping, filtering with aggregates, custom aggregate functions, and performance optimization, please refer to the advanced usage section.
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Assembly for .NET examples, plugins, and showcase
- GroupDocs.Assembly for Java examples, plugins, and showcase
- GroupDocs.Assembly for Python via .NET examples, plugins, and showcase
Along with the full-featured .NET library, we provide simple but powerful free online apps.
To assemble documents from templates and data sources, you can use the online apps from the GroupDocs.Assembly App Product Family.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.