Show / Hide Table of Contents

    IDataStore.GroupBy<TModel>(int level, string sortKey = null) Method

    .NET Standard 2.x

    Generic method. Groups rows of the primary buffer by the level of grouping criteria of DataStore. The sort criteria can be specified.

    Namespace: DWNet.Data

    Assembly: DWNet.Data.dll

    Syntax

      public IEnumerable<IGrouping<object, TModel>> GroupBy<TModel>(int level, string sortKey = null);
    

    Type Parameters

    TModel

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

    Parameters

    level System.Int32

    The level of grouping criteria of DataStore.

    It is the number of a group defined in the DataWindow object which starts from 1.

    sortkey System.String

    A string whose value is the valid sort criteria for the DataStore. The expression includes column names or numbers. A column number must be preceded by a pound sign (#).

    Default is null which indicates it will use the current sort criteria in DataStore.

    Returns

    System.Collections.Generic.IEnumerable<IGrouping<System.Object, TModel>>

    Returns an IEnumerable<IGrouping<Object,IGroupingGetter<TModel>>> object which can be used to read data in groups.

    Examples

    The d_department_course DataStore contains two levels of groups: the primary group is grouped by course_departmentid; the nested group is grouped by course_courseid. The following code example demonstrates how to get the primary and the nested groups and return the TModel data of these groups.

    using Appeon.ApiDoc.Models;
    using DWNet.Data;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Appeon.ApiDoc.IDataStoreExamples
    {
        public class GroupByExample
        {
            private readonly SchoolContext _context;
    
            public GroupByExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example4()
            {
                // Instantiates a DataStore object with datawindow: d_department_course.
                var datastore = new DataStore("d_department_course", _context);
    
                datastore.Retrieve();
    
                // The primary group is by course_departmentid which has only 4 values.
                var departmentGroup = datastore.GroupBy<D_Department_Course>(1);
    
                Console.WriteLine("Department Group Count: {0}",
                    departmentGroup.Count());
    
                // The nested group is by course_courseid which has only 9 values.
                var courseGroup = datastore.GroupBy<D_Department_Course>(2);
    
                Console.WriteLine("Course Group Count: {0}",
                    courseGroup.Count());
    
                // Changes sort to sort by course_title; then get the primary group again.
                departmentGroup = datastore.GroupBy<D_Department_Course>(1, "course_title");
    
                Console.WriteLine("SortBy course_title, Department Group Count: {0}",
                    departmentGroup.Count());
    
                /*This code produces the following output:
                 
                Department Group Count: 4
                Course Group Count: 9
                SortBy course_title, Department Group Count: 7
                */
            }
        }
    }
    

    Example Refer To

    Model Class: D_Department_Course
    DataWindow File: d_department_course

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon