ISqlWhereAndOr<TBuilder>.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
TBuilder AndWhereExists(ISqlBuilder sqlBuilder);
Parameters
sqlBuilder
SnapObjects.Data.ISqlBuilder
An ISqlBuilder
object which represents a SQL subquery on the right of the EXISTS
operator.
Returns
TBuilder
Returns the TBuilder
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 SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlWhereAndOrExamples
{
public class AndWhereExistsExample
{
private readonly SchoolContext _context;
public AndWhereExistsExample(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 adds an ADN EXISTS condition:
// the data must exist in the result set of the subquery.
sqlbuilder.Select("Title")
.From("Course", "c")
.Where("CourseID", SqlBinaryOperator.GreaterThan,
SqlBuilder.Parameter<int>("courid"))
.AndWhereExists(deptsql);
string sql = sqlbuilder.ToSqlString(_context);
Console.WriteLine(sql);
/*This code produces the following output:
SELECT
[Title]
FROM [Course] AS [c]
WHERE ([CourseID] > @courid
AND EXISTS (SELECT
[DepartmentID]
FROM [Department] AS [d]
WHERE ([d].[DepartmentID] = [c].[DepartmentID]
AND [d].[Name] = @id)))
*/
}
}
}
Applies to
.NET Standard
2.x