Show / Hide Table of Contents

    ISqlHavingBuilder.Having(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter) Method

    .NET Standard 2.x

    Creates a HAVING clause and adds a search condition to the HAVING clause. You can specify a SQL expression on the left of the operator and a SQL parameter on the right of the operator.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      ISqlHavingAndOr Having(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter);
    

    Parameters

    left System.String

    A SQL expression on the left of the operator.

    SqlBinaryOperator SnapObjects.Data.SqlBinaryOperator

    An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.

    parameter SnapObjects.Data.ISqlParameter

    An ISqlParameter object which represents a SQL parameter on the right of the operator.

    It can be created by calling the SqlBuilder.Parameter method.

    Returns

    SnapObjects.Data.ISqlHavingAndOr

    Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.

    Examples

    The following code example demonstrates how to use the Having method to specify the HAVING condition for groups.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlHavingBuilderExamples
    {
        public class HavingExample
        {
            private readonly SchoolContext _context;
    
            public HavingExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example2()
            {
                var sqlQueryBuilder = new SqlQueryBuilder();
    
                // Groups "StudentGrade" table by StudentID and CourseID, and gets the sum of Grade of each group.
                // Specifies the HAVING condition for group to: StudentID equal to the input parameter.
                // The value of input parameter is specified when SQL is executed.                   
                sqlQueryBuilder
                    .Select("StudentID")
                    .Select("sum(Grade)")
                    .From("StudentGrade")
                    .GroupBy("StudentID, CourseID")
                    .Having("StudentID",
                               SqlBinaryOperator.Equals,
                               SqlBuilder.Parameter<int>("studentid"));
    
                // Converts to raw SQL for the database corresponding to the data context.                
                string sql = sqlQueryBuilder.ToSqlString(_context);
    
                Console.WriteLine(sql);
    
                // Executes the SQL and specifies "6" for the input parameter.
                var studentGrade = _context.SqlExecutor.Select<DynamicModel>(sql, 6);
    
                /*This code example produces the following output:           
                  SELECT
                    [StudentID],
                    sum(Grade)
                  FROM [StudentGrade]
                  GROUP BY
                    [StudentID],
                    [CourseID]
                    HAVING ([StudentID] = @studentid)
               */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon