Show / Hide Table of Contents

    ISqlJoinOnAndOr<TBuilder>.AndOn(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder) Method

    .NET Standard 2.x

    Adds the AND logical operator and a search condition to the ON clause; and specifies a SQL expression on the left of the operator and a SQL subquery on the right.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      TBuilder AndOn(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder);
    

    Parameters

    left System.String

    A SQL expression on the left of the operator.

    SqlBinaryOperator SnapObjects.Data.SqlBinaryOperator

    An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.

    sqlBuilder SnapObjects.Data.ISqlBuilder

    An ISqlBuilder object which represents a SQL subquery on the right of the operator.

    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 demonstrates how to join one table with another on multiple conditions. It uses the AndOn method to add more conditions.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlJoinOnAndOrExamples
    {
        public class AndOnExample
        {
            private readonly SchoolContext _context;
    
            public AndOnExample(SchoolContext dataContext)
            {
                // Sets data context.
                _context = dataContext;
            }
    
            public void Example6()
            {
                var sqlQueryBuilder = new SqlQueryBuilder();
    
                // Sets a query to get the largest DepartmentID.
                var sqlBuilder = new SqlQueryBuilder();
                sqlBuilder.SelectRaw("max(DepartmentID)")
                    .From("Department");
    
                // Gets DepartmentID values and Name values from the "Department" table, and Title values from the "Course" table.
                // Sets the condition to join the "Department" table and the "Course" table: 
                // Department.DepartmentID = Course.DepartmentID, and Department.DepartmentID less than the largest DepartmentID returned by the query.             
                sqlQueryBuilder
                    .Select("Department.DepartmentID")
                    .Select("Department.Name")
                    .Select("Course.Title")
                    .From("Department")
                    .Join("Course")
                    .OnRaw("Department.DepartmentID = Course.DepartmentID")
                    .AndOn("Department.DepartmentID",
                        SqlBinaryOperator.LessThan, sqlBuilder);
    
                // Converts to raw SQL for the database corresponding to the data context.
                string sql = sqlQueryBuilder.ToSqlString(_context);
    
                Console.WriteLine(sql);
                
                /*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] < (SELECT
                                                        MAX([DepartmentID])
                                                     FROM [Department])
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon