Show / Hide Table of Contents

    IQueryAndOrBuilder<TModel>.AndWhere(string left, ISqlBuilder sqlBuilder) Method

    .NET Standard 2.x

    Adds the AND logical operator and a search condition to the WHERE clause; and specifies a SQL expression on the left of the operator and a SQL subquery on the right. The operator is '='.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    IQueryAndOrBuilder<TModel> AndWhere(string left, ISqlBuilder sqlBuilder);
    

    Parameters

    left System.String

    A SQL expression on the left of the operator.

    sqlBuilder SnapObjects.Data.ISqlBuilder

    An ISqlBuilder object which represents a SQL subquery on the right of the 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 AndWhere method to add an AND condition to the WHERE clause. In this example, the AND condition uses the result set of a subquery and the default operator "=".

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Appeon.ApiDoc.Models.School;
    using SnapObjects.Data;
    
    namespace Appeon.ApiDoc.IQueryAndOrBuilderExamples
    {
        public class AndWhereExample
        {
            private SchoolContext _context;
            
            public AndWhereExample(SchoolContext dataContext)
            {
                // Sets Data Context
                _context = dataContext;
            }
    
            public async Task<int> Example5()
            {
                // Get a QueryBuilder.
                var Builder = _context.SqlModelMapper.GetQueryBuilder<Person>();
                
                // Defines a subquery. The query result is DepartmentID = 4.
                var subsqlbuilder = new SqlQueryBuilder();
                subsqlbuilder.Select("DepartmentID")
                    .From("Department")
                    .WhereValue("Name", "Economics");
                     
                Builder.Where("PersonID", SqlBinaryOperator.LessThan,
                                SqlBuilder.Parameter<int>("id"))
                       .AndWhere("PersonID", subsqlbuilder);
                
                var result = (await Builder.LoadAsync(20)).ToList();
                
                foreach (var person in result)
                {
                    Console.WriteLine("PersonID:    {0}", person.PersonID);
                    Console.WriteLine("FirstName:   {0}", person.FirstName);
                    Console.WriteLine("LastName:    {0}", person.LastName);
                    Console.WriteLine("HireDate:    {0}", person.HireDate);
                    Console.WriteLine("Discriminator:   {0}", person.Discriminator);
                }
                
                return result.Count;
                
                /*This code produces the following output:
                
                    PersonID:    4
                    FirstName:   Fadi
                    LastName:    Fakhouri
                    HireDate:    8/6/2002 12:00:00 AM
                    Discriminator:   Instructor
                */
            }
        }
    }
    

    Example Refer To

    Model Class: Person

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon