Show / Hide Table of Contents

    SqlAndWhereAttribute Class

    .NET Standard 2.x

    Sets an additional search condition on top of the existing search condition in the model class. One or more SqlAndWhere attributes can be applied to one model class.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Inherited Constructor

    SnapObjects.Data.SqlWhereAttribute

    Syntax

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

    Constructors

    Name Description
    SqlAndWhereAttribute(string rawCondition) Initializes a new instance of the SqlAndWhereAttribute class.
    SqlAndWhereAttribute(string left, string right) Initializes a new instance of the SqlAndWhereAttribute 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 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 SqlAndWhere attribute must be used together with the SqlWhere attribute.

    The SqlAndWhere attribute denotes that a model class should add an AND 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 SqlAndWhere attribute. It applies an SqlAndWhere attribute to the DepartmentSqlWhere2 class, to add the AND logical operator and a search condition "Administrator = 2" 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.SqlAndWhereAttributeExamples
    {
        public class SqlAndWhereAttributeExample
        {
            private SchoolContext _context;
    
            public SqlAndWhereAttributeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                // Gets the SQL statement.
                string sql = ModelSqlBuilder.
                    GetBuilder<DepartmentSqlWhere2>(_context).QuerySql;
    
                // Executes the SQL statement.
                var department = _context.SqlExecutor.Select<DepartmentSqlWhere2>(sql);
    
                Console.WriteLine("SQL statement:");
                Console.WriteLine(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