Show / Hide Table of Contents

    ISqlWhereBuilder.WhereNotExists(ISqlBuilder sqlBuilder) Method

    .NET Standard 2.x

    Creates a WHERE clause, and adds a search condition to the WHERE clause. Uses the NOT EXISTS operator to test if the record returned from a subquery does not exist.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      ISqlJoinOnBuilder LeftJoin(string table);
    

    Parameters

    sqlBuilder SnapObjects.Data.ISqlBuilder

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

    Returns

    SnapObjects.Data.ISqlWhereAndOr

    Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.

    Examples

    The following code example uses the WhereNotExists method to specify that the "Title" value of the "Course" table must not exist in the result set of the subquery.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlWhereBuilderExamples
    {
        public class WhereNotExistsExample
        {
            private readonly SchoolContext _context;
    
            public WhereNotExistsExample(SchoolContext dataContext)
            {
                // Sets data context.
                _context = dataContext;
            }
    
            public void Example()
            {
    
                // Declares SqlQueryBuilder.
                var sqlbuilder = new SqlQueryBuilder();
    
                // Defines a subquery.
                var deptsql = new SqlQueryBuilder();
                deptsql.Select("DepartmentID")
                    .From("Department", "d")
                    .Where("d.DepartmentID", "c.DepartmentID")
                    .AndWhere("d.Name", SqlBuilder.Parameter<string>("id"));
    
                // Defines a SQL statement, and specifies the WhereNotExists condition:
                // data cannot exist in the result set of the subquery.
                sqlbuilder.Select("Title")
                    .From("Course", "c")
                    .WhereNotExists(deptsql);
    
                string sql = sqlbuilder.ToSqlString(_context);
    
                Console.WriteLine(sql);
    
                /*This code produces the following output:
    
                SELECT
                [Title]
                FROM [Course] AS [c]
                WHERE NOT EXISTS (SELECT
                [DepartmentID]
                FROM [Department] AS [d]
                WHERE ([d].[DepartmentID] = [c].[DepartmentID]
                AND [d].[Name] = @id)))
               */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon