Show / Hide Table of Contents

    ISqlHavingAndOr<TBuilder>.OrHaving(ISqlHavingCondition condition) Method

    .NET Standard 2.x

    Adds the OR logical operator and a search condition (specified by an ISqlWhereCondition object) to the HAVING clause.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      TBuilder OrHaving(ISqlHavingCondition condition);
    

    Parameters

    condition SnapObjects.Data.ISqlHavingCondition

    An ISqlHavingCondition object which represents a search condition of the HAVING clause.

    This object can be created by using the SqlBuilder.Having method and so on.

    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 OrHaving method to add an OR condition to the HAVING clause.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlHavingAndOrExamples
    {
        public class OrHavingExample
        {
            private readonly SchoolContext _context;
    
            public OrHavingExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example7()
            {
                var sqlQueryBuilder = new SqlQueryBuilder();
    
                // Defines a HAVING condition.
                var having = SqlBuilder.HavingRaw("StudentID = 8");
    
                // Groups "StudentGrade" table by StudentID and CourseID, and gets the sum of Grade of each group.
                // Specifies the HAVING condition for group as "StudentID = 6" or the condition defined above (StudentID = 8).
                sqlQueryBuilder
                    .Select("StudentID")
                    .Select("sum(Grade)")
                    .From("StudentGrade")
                    .GroupBy("StudentID, CourseID")
                    .HavingRaw("StudentID = 6")
                    .OrHaving(having);
    
                // 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]
                  GROUP BY
                    [StudentID],
                    [CourseID]
                    HAVING ([StudentID] = 6
                    OR ([StudentID] = 8))
               */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon