SqlHavingAttribute Class
.NET Standard 2.x |  Current Version (1.0.1) 
Denotes that a model class should use a search condition for a group or an aggregate.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Inherited Constructor
SnapObjects.Data.SqlWhereAttribute
Syntax
   [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
   public class SqlHavingAttribute : SqlWhereAttribute
Constructors
| Name | Description | 
|---|---|
| SqlHavingAttribute(string rawCondition) | Initializes a new instance of the SqlHavingAttributeclass. | 
| SqlHavingAttribute(string left, string right) | Initializes a new instance of the SqlHavingAttributeclass. | 
Properties
| Name | Return Type | Description | 
|---|---|---|
| Left | string | Gets the raw SQL expression on the left of the operator. | 
| Operator | SqlBinaryOperator | Gets or sets the operator to test the two expression on the left and right. | 
| RawCondition | string | Gets the string of the raw SQL search condition if it has been specified in the constructor. | 
| RawConditionDefined | bool | Gets whether the raw SQL search condition has been specified in the constructor. | 
| Right | string | Gets the raw SQL expression on the right of the operator. | 
Remarks
The SqlHaving attribute is typically used with the SqlGroupBy attribute.
The SqlHaving attribute denotes that a model class should use a HAVING clause in the SQL SELECT statement to define the search condition for a group or an aggregate.
Examples
The following code example demonstrates how to use the SqlHaving attribute. It applies the SqlHaving attribute to the StudentGradeSqlHaving class to add a HAVING clause to the SQL SELECT statement, to define the search condition for the group.
Model: StudentGradeSqlHaving
using SnapObjects.Data;
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace Appeon.ApiDoc.Models.School
{
    /// <summary>
    /// This model class maps to the dbo.StudentGrade table.
    /// </summary>
    [Table("StudentGrade", Schema = "dbo")]
    [SqlGroupBy("StudentID")]
    [SqlHaving("Sum(Grade) is not null")]
    [SqlAndHaving("Sum(Grade) < 5")]
    [SqlAndHaving("Sum(Grade)", "2", Operator = SqlBinaryOperator.GreaterThan)]
    public class StudentGradeSqlHaving
    {
        public Int32 StudentID { get; set; }
        [SqlCompute(alias: "SumGrade", expression: "Sum(Grade)")]
        public decimal? SumGrade { get; set; }
    }
}
Example Method:
using SnapObjects.Data;
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.SqlHavingAttributeExamples
{
    public class SqlHavingAttributeExample
    {
        private SchoolContext _context;
        public SqlHavingAttributeExample(SchoolContext dataContext)
        {
            // Sets the data context.
            _context = dataContext;
        }
        public void Example()
        {
            // Gets the SQL statement.
            string sql = ModelSqlBuilder.GetBuilder<StudentGradeSqlHaving>(_context).QuerySql;
            Console.WriteLine("SQL statement:");
            Console.WriteLine(sql);
            // Executes the SQL statement.
            var resultSet = _context.SqlExecutor.Select<StudentGradeSqlHaving>(sql);
            /* This code example produces the following output:
            
            SQL statement:
            SELECT
             [StudentID],
            SUM([Grade]) AS [SumGrade]
            FROM [dbo].[StudentGrade]
            GROUP BY
             [StudentID]
            HAVING (SUM([Grade]) IS NOT NULL
            AND SUM([Grade]) < 5
            AND SUM([Grade]) > = 2)
            */
        }
    }
}
Applies to
.NET Standard
2.x