Files to look at:
- Form1.cs (VB: Form1.vb)
- CustomAggregates (VB: CustomAggregates)
- DataAccess (VB: DataAccess)
In addition to predefined aggregates (Sum, Count, Min, Max, Avg, Single, Exists), XPO users can implement custom aggregate functions. You can use them to query data with XPQuery and data sources that support CriteriaOperator (including server mode collections).
This example illustrates how to use the CountDistinct and STDEVP custom aggregates with XPView and XPQuery (research the Form1 code behind and designer files).
// Criteria string for a custom aggregate with a detail collection property or Free Joins.
"YourCollection.YourCustomAggregate(YourNestedProperty)"
// Specific criteria string examples for the Orders collection and the CountDistinct and STDEVP custom aggregates.
"[Orders][].CountDistinct([ProductName])", "[Orders][].STDEVP([Price])"
// Criteria string for a custom aggregate with a top-level collection of persistent objects.
"[].CUSTOM_AGGREGATE('YourCustomAggregate', YourNestedProperty)"
// LINQ to XPO usage of custom aggregates (CountDistinct and STDEVP) with a detail collection property.
new XPQuery<Customer>(theSession)
.Select(t => new {
ContactName = t.ContactName,
DistinctProducts = (int)CountDistinctCustomAggregate.CountDistinct(
t.Orders, o => o.ProductName
),
QuantityVariance = (double?)STDEVPCustomAggregate.STDEVP(
t.Orders, o => o.Quantity
),
PriceVariance = (double?)STDEVPCustomAggregate.STDEVP(
t.Orders, o => o.Price
),
}).OrderBy(t => t.ContactName).ToList();
To create a custom aggregate, implement the following interfaces: ICustomAggregate
, ICustomAggregateQueryable
, ICustomAggregateFormattable
.
For more information, research the CountDistinctCustomAggregate
and STDEVPCustomAggregate
classes in the XpoCustomAggregate/CustomAggregates folder.
To register a custom aggregate, use the CriteriaOperator.RegisterCustomAggregate
method (see the Form1 constructor).
See Also How to: Implement and Use Custom Aggregate Functions