Show / Hide Table of Contents

    ISqlJoinOnAndOr<TBuilder>.OrOnRaw(string clause, params ISqlParameter[] parameters) Method

    .NET Standard 2.x

    Adds the OR logical operator and a raw SQL to the ON clause.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      TBuilder OrOnRaw(string clause, params ISqlParameter[] parameters);
    

    Parameters

    clause System.String

    A string of raw SQL to be added to the ON clause.

    parameters SnapObjects.Data.ISqlParameter[]

    (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.

    Returns

    TBuilder

    Returns the TBuilder object which can be used to add more search conditions to the current ON clause.

    Examples

    The following code example joins one table with another based on multiple OR conditions. It uses the OrOnRaw method to add the OR condition (in raw SQL).

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlJoinOnAndOrExamples
    {
        public class OrOnRawExample
        {
            private readonly SchoolContext _context;
    
            public OrOnRawExample(SchoolContext dataContext)
            {
                // Sets data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var sqlQueryBuilder = new SqlQueryBuilder();
    
                // Gets DepartmentID values and Name values from the "Department" table, and Title values from the "Course" table.
                // Sets the condition to join the "Department" and "Course" tables:
                // Department.DepartmentID = Course.DepartmentID, and Department.DepartmentID equal to the input argument "1" or the input argument "2".             
                sqlQueryBuilder
                    .Select("Department.DepartmentID")
                    .Select("Department.Name")
                    .Select("Course.Title")
                    .From("Department")
                    .Join("Course")
                    .OnRaw("Department.DepartmentID = Course.DepartmentID")
                    .AndOnRaw("(Department.DepartmentID = @departmentid1 ",
                             SqlBuilder.Parameter<int>("departmentid1"))
                    .OrOnRaw("Department.DepartmentID = @departmentid2)",
                            SqlBuilder.Parameter<int>("departmentid2"));
    
    
                // Converts to raw SQL for the database corresponding to the data context.
                string sql = sqlQueryBuilder.ToSqlString(_context);
    
                Console.WriteLine(sql);
    
                // Executes the SQL statement and specifies the values for the two input arguments.
                var departments = _context.SqlExecutor.Select<DynamicModel>(sql, 1, 2);
    
                /*This code example produces the following output:           
                  SELECT
                    [Department].[DepartmentID],
                    [Department].[Name],
                    [Course].[Title]
                  FROM [Department] JOIN [Course]
                  ON [Department].[DepartmentID] = [Course].[DepartmentID]
                  AND ([Department].[DepartmentID] = @departmentid1
                  OR [Department].[DepartmentID] = @departmentid2)
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon