Show / Hide Table of Contents

    TopAttribute Class

    .NET Standard 2.x | Current Version (1.0.1)

    0.5.0-alpha

    1.0.1 (current)

    Denotes that a model class should limit the rows returned.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Inheritance Constructor

    System.Attribute

    Syntax

       [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
       public class TopAttribute : Attribute
    

    Constructors

    Name Description
    TopAttribute(int number) Initializes a new instance of the TopAttribute class.

    Properties

    Name Return Type Description
    TopRows int Gets the maximum number of rows to be returned.

    Remarks

    The Top attribute denotes that a model class should add a TOP clause to the SQL SELECT statement to limit the rows returned.

    Examples

    The following code example demonstrates how to use the Top attribute. It applies the Top attribute to the DepartmentTop class, to add a TOP clause to the SQL SELECT statement to limit the rows returned.

    Model: DepartmentTop
    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")]
        [Top(2)]
    
        public class DepartmentTop
        {
            [Key]
            public Int32 DepartmentID { get; set; }
    
            public String Name { get; set; }
    
            public Decimal Budget { get; set; }
    
            public DateTime StartDate { get; set; }
    
            public Int32? Administrator { get; set; }
    
        }
    }
    
    Example Method:
    using SnapObjects.Data;
    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.TopAttributeExamples
    {
        public class TopAttributeExample
        {
            private SchoolContext _context;
    
            public TopAttributeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                // Gets the SQL statement.
                string sql = ModelSqlBuilder.GetBuilder<DepartmentTop>(_context).QuerySql;
    
                Console.WriteLine("SQL statement:");
                Console.WriteLine(sql);
    
                // Executes the SQL statement.
                var department = _context.SqlExecutor.Select<DepartmentTop>(sql);
    
                /* This code example produces the following output:
                
                SQL statement:
                SELECT
                 TOP(2) [DepartmentID],
                [Name],
                [Budget],
                [StartDate],
                [Administrator]
                FROM [dbo].[Department]
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon