Show / Hide Table of Contents

    SqlOrWhereAttribute Class

    .NET Standard 2.x

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

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Inheritance Constructor

    SnapObjects.Data.SqlWhereAttribute

    Syntax

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

    Constructors

    Name Description
    SqlOrWhereAttribute(string rawCondition) Initializes a new instance of the SqlOrWhereAttribute class.
    SqlOrWhereAttribute(string left, string right) Initializes a new instance of the SqlOrWhereAttribute 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 search condition has been specified in the constructor.
    Right string Gets the raw SQL expression on the right of the operator.

    Remarks

    The SqlOrWhere attribute must be used together with the SqlWhere attribute.

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

    Examples

    The following code example demonstrates how to use the SqlOrWhere attribute. It applies the SqlOrWhere attribute to the DepartmentSqlWhere2 class, to add the OR logical operator and the search condition "Administrator > 4" to the WHERE clause of the SQL SELECT statement.

    Model: DepartmentSqlWhere2
    using SnapObjects.Data;
    using System;
    using System.ComponentModel.DataAnnotations;
    
    namespace Appeon.ApiDoc.Models.School
    {
        /// <summary>
        /// This model class maps to the dbo.Department table.
        /// </summary>
        [FromTable("Department", Schema = "dbo")]
        [SqlWhere("Budget > 1")]
        [SqlAndWhere("Administrator = 2")]
        [SqlOrWhere("Administrator > 4")]
        [SqlOrderBy("DepartmentID asc")]
        public class DepartmentSqlWhere2
        {
            [Key]
            public int DepartmentID { get; set; }
    
            public string Name { get; set; }
    
            public decimal Budget { get; set; }
    
            public DateTime StartDate { get; set; }
    
            public int? Administrator { get; set; }
    
        }
    }
    
    Example Method:
    using SnapObjects.Data;
    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.SqlOrWhereAttributeExamples
    {
        public class SqlOrWhereAttributeExample
        {
            private SchoolContext _context;
    
            public SqlOrWhereAttributeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                // Gets the SQL statement.
                string sql = ModelSqlBuilder.GetBuilder<DepartmentSqlWhere2>(_context).QuerySql;
    
                Console.WriteLine("SQL statement:");
                Console.WriteLine(sql);
    
                // Executes the SQL statement.
                var departments = _context.SqlExecutor.Select<DepartmentSqlWhere2>(sql);
    
                /* This code example produces the following output:
                
                SQL statement:
                SELECT
                 [DepartmentID],
                [Name],
                [Budget],
                [StartDate],
                [Administrator]
                FROM [dbo].[Department]
                WHERE ([Budget] > 1
                AND [Administrator] = 2
                OR [Administrator] > 4)
                ORDER BY
                 [DepartmentID] ASC
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon