Show / Hide Table of Contents

    ISqlHavingAndOr<TBuilder>.AndHaving(string left, ISqlParameter parameter) Method

    .NET Standard 2.x

    Adds the AND logical operator and a search condition to the HAVING clause. Specifies a SQL expression to the left of the operator and a SQL parameter to the right of the operator. The operator is '='.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      TBuilder AndHaving(string left, ISqlParameter parameter);
    

    Parameters

    left System.String

    A SQL expression on the left of the operator.

    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

    TBuilder

    Returns the TBuilder 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 AndHaving method to add an AND condition to the HAVING clause.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlHavingAndOrExamples
    {
        public class AndHavingExample
        {
            private readonly SchoolContext _context;
    
            public AndHavingExample(SchoolContext dataContext)
            {
                // Sets the data context
                _context = dataContext;
            }
    
            public void Example1()
            {
                var sqlQueryBuilder = new SqlQueryBuilder();
    
                // Groups "StudentGrade" table by StudentID and CourseID columns, and gets the sum of Grade of each group.
                // Specifies the HAVING condition for the group as "StudentID > 0" and CourseID equal to the input parameter.
                // The value of input parameter is specified when the SQL is executed.
                sqlQueryBuilder
                    .Select("StudentID")
                    .Select("sum(Grade)")
                    .From("StudentGrade")
                    .GroupBy("StudentID, CourseID")
                    .HavingRaw("StudentID > 0")
                    .AndHaving("CourseID", SqlBuilder.Parameter<int>("courseid"));
    
                // 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 "2030" for the input parameter.
                var studentGrade = _context.SqlExecutor.Select<DynamicModel>(sql, 2030);
    
                /*This code example produces the following output:           
                SELECT
                    [StudentID],
                    sum(Grade)
                FROM [StudentGrade]
                GROUP BY
                    [StudentID],
                    [CourseID]
                    HAVING ([StudentID] > 0
                    AND [CourseID] = @courseid)
               */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon