Show / Hide Table of Contents

    ISqlWhereBuilder.AsGroupBy() Method

    .NET Standard 2.x

    Returns an ISqlGroupBuilder object which Represents an object used to build the GROUP BY clause in a SQL statement.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      ISqlGroupBuilder AsGroupBy();
    

    Returns

    SnapObjects.Data.ISqlGroupBuilder

    Returns the ISqlGroupBuilder object which Represents an object used to build the GROUP BY clause in a SQL statement.

    Examples

    The following code example uses the AsGroupBy method to group records in the "Course" table by DepartmentID and Credits, and retrieves records whose count is greater than 2.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlWhereBuilderExamples
    {
        public class AsGroupByExample
        {
            private readonly SchoolContext _context;
    
            public AsGroupByExample(SchoolContext dataContext)
            {
                // Sets data context.
                _context = dataContext;
            }
    
            public void Example()
            {
    
                // Declares SqlQueryBuilder.
                var sqlbuilder = new SqlQueryBuilder();
                
                // Defines a SQL statement and groups the records by DepartmentID.
                sqlbuilder.Select("DepartmentID")
                    .SelectRaw("Count(*)")
                    .From("Course")
                    .GroupBy("DepartmentID")
                    .HavingRaw("Count(*) > 2");
    
                // Adds a GroupBy condition: Credits
                sqlbuilder.AsGroupBy().GroupBy("Credits");
    
                string sql = sqlbuilder.ToSqlString(_context);
    
                Console.WriteLine(sql);
    
                /*This code produces the following output:
    
                SELECT
                [DepartmentID],
                COUNT(*)
                FROM [Course]
                GROUP BY
                [DepartmentID],
                [Credits]
                HAVING (COUNT(*) > 2)
               */
    
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon