List grouping and aggregation PivotData C# Examples
List grouping is usually performed with LINQ:
Customer[] customers; // lets assume Customer has Category and IsActive properties var groupedCustomers = customers.GroupBy( c => new { c.Category, c.IsActive } ); foreach (var grp in groupedCustomers) { Console.WriteLine("Category={0} IsActive={1} Count={2}", grp.Key.Category, grp.Key.IsActive, grp.Count() ); }
In some cases number and names of grouping columns are dynamic and determined at the run-time, and instead of LINQ you can use PivotData library:
var pvtData = new PivotData(new [] {"Category","IsActive"}, new CountAggregatorFactory() ); pvtData.ProcessData( customers, new ObjectMember().GetValue ); foreach (var grp in pvtData) { Console.WriteLine("Category={0} IsActive={1} Count={2}", grp.Key[0], grp.Key[1], grp.Value.Count );
PivotData
class provide simple API for accessing aggregated values (see
cube basics for more details).
Instead of "count" other
aggregate functions
may be used (sum, average, min, max etc).
Unless ListAggregatorFactory
is used, PivotData doesn't keep references to objects from the list;
it can process very large datasets and calculate only measures configured with aggregator factory.
ObjectMember
component provides fast access to the object's member (properties, fields) and can be used with any typed list or collection.
You may provide your own value accessor delegate to get fastest possible performance.
Complete example code is here: NReco.PivotData github repository