Show / Hide Table of Contents

    SqlOrHavingAttribute Class

    .NET Standard 2.x | Current Version (1.0.1)

    0.5.0-alpha

    1.0.1 (current)

    Sets an optional search condition on top of the existing search condition for a group or an aggregate in the model class. Either condition must be met. One or more SqlOrHaving attributes can be applied to one model class.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Inheritance Constructor

    SnapObjects.Data.SqlHavingAttribute

    Syntax

       [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
       public class SqlOrHavingAttribute : SqlHavingAttribute
    

    Constructors

    Name Description
    SqlOrHavingAttribute(string rawCondition) Initializes a new instance of the SqlOrHavingAttribute class.
    SqlOrHavingAttribute(string left, string right) Initializes a new instance of the SqlOrHavingAttribute class.

    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 expressions 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 SqlOrHaving attribute must be used together with the SqlHaving attribute.

    The SqlOrHaving attribute denotes that a model class should add an OR logical operator and another search condition in the HAVING clause of the SQL SELECT statement.

    Examples

    The following code example demonstrates how to use the SqlOrHaving attribute. It applies the SqlOrHaving attribute to the StudentGradeSqlOrHaving class, to add the OR logical operator and a search condition to the HAVING clause.

    Model: StudentGradeSqlOrHaving
    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 null")]
        [SqlOrHaving("Sum(Grade)", "5", Operator = SqlBinaryOperator.LessThan)]
    
        public class StudentGradeSqlOrHaving
        {
            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.SqlOrHavingAttributeExamples
    {
        public class SqlOrHavingAttributeExample
        {
            private SchoolContext _context;
    
            public SqlOrHavingAttributeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                // Gets the SQL statement.
                string sql = ModelSqlBuilder.GetBuilder<StudentGradeSqlOrHaving>(_context).QuerySql;
    
                Console.WriteLine("SQL statement:");
                Console.WriteLine(sql);
    
                // Executes the SQL statement.
                var resultSet = _context.SqlExecutor.Select<StudentGradeSqlOrHaving>(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 NULL
                OR SUM([Grade]) < 5)
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon