Show / Hide Table of Contents

    ISqlGroupBuilder.AsHaving() Method

    .NET Standard 2.x

    Returns an ISqlHavingAndOr object that can be used to add a search condition to a group or an aggregate in the HAVING clause.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      ISqlHavingAndOr AsHaving();
    

    Returns

    SnapObjects.Data.ISqlHavingAndOr

    Returns the ISqlHavingAndOr object that can add a search condition to a group or an aggregate in the HAVING clause.

    Examples

    The following code example demonstrates how to use the AsHaving method to get the existing HAVING condition and add a condition to it.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlGroupBuilderExamples
    {
        public class AsHavingExample
        {
            private readonly SchoolContext _context;
    
            public AsHavingExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var sqlQueryBuilder = new SqlQueryBuilder();
    
                // Groups "StudentGrade" table by StudentID, and gets the sum of Grade of each group.
                // Specifies the HAVING condition for the group as "StudentID > 0".
                sqlQueryBuilder
                    .Select("StudentID")
                    .Select("sum(Grade)")
                    .From("StudentGrade")
                    .GroupBy("StudentID")
                    .HavingRaw("StudentID > 0");
    
                // Gets the current HAVING condition and adds a new condition: StudentID < 10.
                sqlQueryBuilder.AsHaving().AndHavingRaw("StudentID < 10");
    
                // Converts to raw SQL for the database corresponding to the data context.
                string sql = sqlQueryBuilder.ToSqlString(_context);
    
                Console.WriteLine(sql);  
    
                /*This code example produces the following output:           
                SELECT
                    [StudentID],
                    sum(Grade)
                FROM [StudentGrade]
                WHERE ([StudentID] <> 0)
                GROUP BY
                    [StudentID]
                    HAVING ([StudentID] > 0
                    AND [StudentID] < 10)
               */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon