Show / Hide Table of Contents

    IDataStore.Avg<TModel>(Func<TModel, decimal> selector, Predicate<TModel> predicate) Method

    .NET Standard 2.x

    Computes the average of a sequence of decimal values. These values are obtained by invoking a transform function on each row in the primary buffer of the DataStore which has been filtered by a predicate.

    Namespace: DWNet.Data

    Assembly: DWNet.Data.dll

    Syntax

      public decimal Avg<TModel>(Func<TModel, decimal> selector, Predicate<TModel> predicate);
    

    Type Parameters

    TModel

    The type of a model class that matches with the current DataObject.

    Parameters

    selector System.Func<TModel, decimal>

    A transform function to apply to each row.

    predicate System.Predicate<TModel>

    A function to evaluate each row in the primary buffer for a condition.

    Returns

    System.decimal

    The average of the sequence of values.

    Examples

    The following code example demonstrates how to use the Avg<TModel>(Func<TModel, decimal>, Predicate<TModel>) method to compute the average budget of all departments.

    using Appeon.ApiDoc.Models;
    using DWNet.Data;
    using System;
    
    namespace Appeon.ApiDoc.IDataStoreExamples
    {
        public class AvgExample
        {
            private readonly SchoolContext _context;
    
            public AvgExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example2()
            {
                // Instantiates a DataStore object with datawindow: d_department.
                var datastore = new DataStore("d_department", _context);
    
                datastore.Retrieve();
    
                // This datastore has 4 records
                // but after filtered by Budget > 200000.00
                // values of Budget are:
                // 350000.00, 250000.00
                // so avg(Budget) is 300000.00
                decimal avg = datastore.Avg<D_Department>(
                    o => o.Budget,
                    o => o.Budget > 200000.00m);
    
                Console.WriteLine(
                    "The Avg(Budget) for records " +
                    "whose Budget is larger than 200000.00 is: {0}",
                    avg);
    
                /*This code produces the following output:
                
                The Avg(Budget) for records whose Budget is larger than 200000.00 is: 
                300000.0000
                */
            }
        }
    }
    

    Example Refer To

    Model Class: D_Department
    DataWindow File: d_department

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon