Show / Hide Table of Contents

    SqlGroupByAttribute Class

    .NET Standard 2.x

    Denotes that a model class should divide the query result into groups of rows.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Inherited Constructor

    System.Attribute

    SnapObjects.Data.ISqlGroupBy

    Syntax

       [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
       public class SqlGroupByAttribute : Attribute, ISqlGroupBy
    

    Constructors

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

    Properties

    Name Return Type Description
    GroupByExpressions IReadOnlyList<string> Gets one or more raw SQL expressions after the keyword GROUP BY.

    Remarks

    The SqlGroupBy attribute denotes that a model class should use a GROUP BY clause in the SQL SELECT statement to divide the query results into several groups of rows.

    Examples

    The following code example demonstrates how to use the SqlGroupBy attribute. It applies the SqlGroupBy attribute to the StudentGradeSqlHaving class to add 'GROUP BY StudentID' in the SQL SELECT statement, to divide the query results into several groups of rows by StudentID.

    You can specify more than one raw SQL expressions to the expression property of the SqlGroupBy attribute. Each expression specifies a column or a non-aggregate calculation on a column.

    Example Method:
    using SnapObjects.Data;
    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.SqlGroupByAttributeExamples
    {
        public class SqlGroupByAttributeExample
        {
            private SchoolContext _context;
    
            public SqlGroupByAttributeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                // Gets the SQL statement.
                string sql = ModelSqlBuilder.GetBuilder<StudentGradeSqlHaving>(_context).QuerySql;
    
                Console.WriteLine("SQL statement:");
                Console.WriteLine(sql);
    
                // Executes the SQL statement.
                var resultSet = _context.SqlExecutor.Select<StudentGradeSqlHaving>(sql);
    
                /* This code example produces the following output:
                
                SQL statement:
                SELECT
                 [StudentID],
                SUM([Grade]) AS [SumGrade]
                FROM [dbo].[StudentGrade]
                GROUP BY
                 [StudentID]
                HAVING (SUM([Grade]) IS NOT NULL
                AND SUM([Grade]) < 5
                AND SUM([Grade]) > = 2)
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon