ISqlWhereAndOr<TBuilder>.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
TBuilder 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
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 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 SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlWhereAndOrExamples
{
public class AndWhereExample
{
private readonly SchoolContext _context;
public AndWhereExample(SchoolContext dataContext)
{
// Sets data context.
_context = dataContext;
}
public void Example5()
{
// Declares SqlQueryBuilder.
var sqlbuilder = new SqlQueryBuilder();
// Defines a subquery.
var deptsql = new SqlQueryBuilder();
deptsql.Select("DepartmentID")
.From("Department")
.Where("Name", SqlBuilder.Parameter<string>("id"));
// Defines a SQL statement and adds an AND condition to WHERE clause:
// a.DepartmentID equal to the result of a subquery
sqlbuilder.Select("Title")
.From("Course", "a")
.Where("CourseID", SqlBinaryOperator.GreaterThan,
SqlBuilder.Parameter<int>("courid"))
.AndWhere("a.DepartmentID", deptsql);
string sql = sqlbuilder.ToSqlString(_context);
Console.WriteLine(sql);
/*This code produces the following output:
SELECT
[Title]
FROM [Course] AS [a]
WHERE ([CourseID] > @courid
AND [a].[DepartmentID] = (SELECT
[DepartmentID]
FROM [Department]
WHERE ([Name] = @id)))
*/
}
}
}
Applies to
.NET Standard
2.x