IDataStore<TModel>.Average(Func<TModel, int?> selector) Method
.NET Standard 2.x
Computes the average of a sequence of int?
values.
Namespace: DWNet.Data
Assembly: DWNet.Data.dll
Syntax
double? Average(Func<TModel, int?> selector);
Parameters
selector
Func<TModel, int?>
A transform function to apply to each row.
Returns
Nullable<Double>
The average of the sequence of values.
Examples
The following code example demonstrates how to use the Average(Func<TModel, int?>)
method to compute the average budget of all departments.
using Appeon.ApiDoc.Models;
using DWNet.Data;
using System;
namespace Appeon.ApiDoc.IDataStore_GenericExamples
{
public class AverageExample
{
private readonly SchoolContext _context;
public AverageExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example5()
{
// Instantiates a DataStore object with datawindow: d_department.
var datastore = new DataStore<D_Department>(_context);
datastore.Retrieve();
// This datastore has 4 records
// Values of Budget are:
// 350000.00, 120000.00, 200000.00, 250000.00
// so avg(Budget) is 230000.00
double? avg = datastore.Average(a => (int?)a.Budget);
Console.WriteLine(
"The average budget of all records Avg(Budget) is: {0}",
avg);
/*This code produces the following output:
The average budget of all records Avg(Budget) is: 230000.0000
*/
}
}
}
Example Refer To
Model Class: D_Department
DataWindow File: d_department