Show / Hide Table of Contents

    SqlOrderByAttribute Class

    .NET Standard 2.x

    Specifies how a model class sorts the result set when loading data.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Inheritance Constructor

    System.Attribute

    Syntax

       [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
       public class SqlOrderByAttribute : Attribute, ISqlOrderBy
    

    Constructors

    Name Description
    SqlOrderByAttribute(params string[] expressions) Initializes a new instance of the SqlOrderByAttribute class.

    Properties

    Name Return Type Description
    OrderByExpressions IReadOnlyList<string> Gets one or more raw SQL expressions in the ORDER BY clause after the keyword ORDER BY.

    Remarks

    The SqlOrderBy attribute denotes that a model class should use the ORDER BY clause in the SQL SELECT statement to sort the result set of the query.

    Examples

    The following code example demonstrates how to use the SqlOrderBy attribute. It applies the SqlOrderBy attribute to the DepartmentSqlWhere2 class, to specify the ORDER BY clause in the SQL SELECT statement to sort the result set of the query.

    Model: DepartmentSqlWhere2
    using SnapObjects.Data;
    using System;
    using System.ComponentModel.DataAnnotations;
    
    namespace Appeon.ApiDoc.Models.School
    {
        /// <summary>
        /// This model class maps to the dbo.Department table.
        /// </summary>
        [FromTable("Department", Schema = "dbo")]
        [SqlWhere("Budget > 1")]
        [SqlAndWhere("Administrator = 2")]
        [SqlOrWhere("Administrator > 4")]
        [SqlOrderBy("DepartmentID asc")]
        public class DepartmentSqlWhere2
        {
            [Key]
            public int DepartmentID { get; set; }
    
            public string Name { get; set; }
    
            public decimal Budget { get; set; }
    
            public DateTime StartDate { get; set; }
    
            public int? Administrator { get; set; }
    
        }
    }
    
    Example Method:
    using SnapObjects.Data;
    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.SqlOrderByAttributeExamples
    {
        public class SqlOrderByAttributeExample
        {
            private SchoolContext _context;
    
            public SqlOrderByAttributeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                // Gets the SQL statement.
                string sql = ModelSqlBuilder.GetBuilder<DepartmentSqlWhere2>(_context).QuerySql;
    
                Console.WriteLine("SQL statement:");
                Console.WriteLine(sql);
    
                // Executes the SQL statement.
                var departments = _context.SqlExecutor.Select<DepartmentSqlWhere2>(sql);
    
                /* This code example produces the following output:
                
                SQL statement:
                SELECT
                 [DepartmentID],
                [Name],
                [Budget],
                [StartDate],
                [Administrator]
                FROM [dbo].[Department]
                WHERE ([Budget] > 1
                AND [Administrator] = 2
                OR [Administrator] > 4)
                ORDER BY
                 [DepartmentID] ASC
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon