Show / Hide Table of Contents

    IDataStore<TModel>.Average(Func<TModel, double> selector) Method

    .NET Standard 2.x

    Computes the average of a sequence of double values.

    Namespace: DWNet.Data

    Assembly: DWNet.Data.dll

    Syntax

    double Average(Func<TModel, double> selector);
    

    Parameters

    selector Func<TModel, double>

    A transform function to apply to each row.

    Returns

    System.Double

    The average of the sequence of values.

    Examples

    The following code example demonstrates how to use the Average(Func<TModel, double>) 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 Example3()
            {
                // 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 => (double)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

    Back to top Generated by Appeon