Show / Hide Table of Contents

    SqlBuilder.EmbeddedWhereExists(ISqlBuilder sqlBuilder) Method

    .NET Standard 2.x

    Creates an ISqlWhereCondition object which represents a search condition that can be used (enclosed in parentheses) when building the WHERE clause. Uses EXISTS to test for the existence of the record returned from a subquery.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

     public static ISqlWhereCondition EmbeddedWhereExists(ISqlBuilder sqlBuilder);
    

    Parameters

    sqlBuilder SnapObjects.Data.ISqlBuilder

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

    Returns

    SnapObjects.Data.ISqlWhereCondition

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

    Remarks

    The newly created search condition is enclosed in parentheses. If the returned ISqlWhereCondition object is used to add another search condition, both of these search conditions will be enclosed in the same pair of parentheses.

    Examples

    The following code example demonstrates how to use the EmbeddedWhereExists method to determine whether the data in the current table exists in the result set of the subquery. If yes, returns the result to a model.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.SqlBuilderExamples
    {
        public class EmbeddedWhereExistsExample
        {
            private SchoolContext _context;
    
            public EmbeddedWhereExistsExample(SchoolContext dataContext)
            {
                // Sets Data Context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var sqlquerybuilder = new SqlQueryBuilder();
    
                // Defines a subquery.
                var subsqlbuilder = new SqlQueryBuilder();
                subsqlbuilder.Select("1")
                    .From("Department")
                    .Where("departmentid", "c.departmentid");
    
                // Defines an EmbeddedWhereExists condition.
                var where = SqlBuilder.EmbeddedWhereExists(subsqlbuilder);
    
                // Creates a SQL statement.
                var query = sqlquerybuilder
                    .Select("*")
                    .From("Course", "c")
                    .Where(where);
    
                // Converts to raw SQL for the database corresponding to the data context.
                string sql = query.ToSqlString(_context);
    
                Console.WriteLine(sql);
                
                /*This code example produces the following output:   
                 
                SELECT
                *
                FROM [Course] AS [c]
                WHERE ( EXISTS (SELECT
                1
                FROM [Department]
                WHERE ([departmentid] = [c].[departmentid])))
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon