Show / Hide Table of Contents

    IQueryAndOrBuilder<TModel>.AndWhereExists(ISqlBuilder sqlBuilder) Method

    .NET Standard 2.x

    Adds the AND logic operator and a search condition to the WHERE clause; and uses EXISTS to test for the existence of the record returned from a subquery.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      IQueryAndOrBuilder<TModel> AndWhereExists(ISqlBuilder sqlBuilder);
    

    Parameters

    sqlBuilder SnapObjects.Data.ISqlBuilder

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

    Returns

    IQueryAndOrBuilder<TModel>

    Returns anIQueryAndOrBuilder<TModel> object which can be used to add more search conditions to the current WHERE clause.

    Examples

    The following code example demonstrates how to use the AndWhereExists method to add an AND EXISTS condition to the WHERE clause. In this example, the EXISTS condition specifies that the data must exist in the result set of a subquery.

    using Appeon.ApiDoc.Models.School;
    using SnapObjects.Data;
    using System;
    using System.Threading.Tasks;
    
    namespace Appeon.ApiDoc.IQueryAndOrBuilderExamples
    {
        public class AndWhereExistsExample
        {
            private SchoolContext _context;
            
            public AndWhereExistsExample(SchoolContext dataContext)
            {
                // Sets Data Context
                _context = dataContext;
            }
    
            public async Task<int> Example() 
            {
                // Get a QueryBuilder.
                var Builder = _context.SqlModelMapper.GetQueryBuilder<Department>();
                
                // Defines subquery.
                var subsqlbuilder = new SqlQueryBuilder();
                subsqlbuilder.Select("1")
                    .From("Department")
                    .Where("departmentid", SqlBuilder.Parameter<int>("id"));
                    
                var subsqlbuilder2 = new SqlQueryBuilder();
                subsqlbuilder2.Select("1")
                    .From("Department")
                    .Where("departmentid", SqlBuilder.Parameter<int>("id2"));
                    
                // Specifies the WHERE EXIST condition:
                // the data exists in the result set of the subquery.
                Builder.WhereExists(subsqlbuilder)
                       .AndWhereExists(subsqlbuilder2);
                       
                // Data with departmentID = 4 and departmentID = 4 are exists.
                var result = (await Builder.LoadAsync(4, 7)).ToList();
                
                if (result.Count != 0) 
                {
                    // If the results of the two sub-queries are true, the data can be loaded.
                    Console.WriteLine("Both subquery results are true.");
                }
                else 
                {
                    // If one of the two subquery results is false, the data cannot be loaded.
                    Console.WriteLine("The results of the two subqueries are not all true.");
                }
                
                return result.Count;
                
                /*This code example produces the following output:   
                 
                    Both subquery results are true.
                */
            }
        }
    }
    

    Example Refer To

    Model Class: Department

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon