Show / Hide Table of Contents

    IDataStore<TModel>.Sort<TKey>(Func<TModel, TKey> keySelector, bool descending = false) Method

    .NET Standard 2.x

    Sorts the rows in the DataStore using the current sort criteria (by the expression).

    Namespace: DWNet.Data

    Assembly: DWNet.Data.dll

    Syntax

    void Sort<TKey>(Func<TModel, TKey> keySelector, bool descending = false);
    

    Type Parameters

    TKey

    Parameters

    keySelector Func<TModel, TKey>

    A transform function to apply to each row.

    descending System.Boolean

    Whether to sort in descending order.

    Remarks

    The null value comes first in the ascending order, and ranks last in the descending order.

    Examples

    The following code example sorts data records by budget in the descending order.

    using Appeon.ApiDoc.Models;
    using DWNet.Data;
    using System;
    
    namespace Appeon.ApiDoc.IDataStore_GenericExamples
    {
        public class SortExample
        {
            private readonly SchoolContext _context;
    
            public SortExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example()
            {
                // Instantiates a DataStore object with datawindow: d_department.
                var datastore = new DataStore<D_Department>(_context);
    
                datastore.Retrieve();
    
                foreach (var item in datastore)
                {
                    Console.WriteLine("Before Sort, Departmentid:{0}", ((D_Department)item).Departmentid);
                }
    
                // Sorts DataStore data in descending order.
                datastore.Sort(a => a.Departmentid, true);
    
                foreach (var item in datastore)
                {
                    Console.WriteLine("After Sort, Departmentid:{0}", ((D_Department)item).Departmentid);
                }
    
                /*This code produces the following output:
                
                Before Sort, Departmentid: 1
                Before Sort, Departmentid: 2
                Before Sort, Departmentid: 4
                Before Sort, Departmentid: 7
                After Sort, Departmentid: 7
                After Sort, Departmentid: 4
                After Sort, Departmentid: 2
                After Sort, Departmentid: 1
                */
            }
        }
    }
    

    Example Refer To

    Model Class: D_Department
    DataWindow File: d_department

    Back to top Generated by Appeon