ISqlWhereBuilder.Where(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder) Method
.NET Standard 2.x
Creates a WHERE clause, and adds a search condition to the WHERE clause. Specifies a SQL expression on the left of the operator and a SQL subquery on the right.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
ISqlWhereAndOr Where(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder);
Parameters
left
System.String
A SQL expression on the left of the operator.
SqlBinaryOperator
SnapObjects.Data.SqlBinaryOperator
An enumeration value of SqlBinaryOperator
, which is the operator to test the two expressions on the left and right.
sqlBuilder
SnapObjects.Data.ISqlBuilder
An ISqlBuilder
object which represents a SQL subquery on the right of the operator.
Returns
SnapObjects.Data.ISqlWhereAndOr
Returns the ISqlWhereAndOr
object which can be used to add more search conditions to the current WHERE clause.
Examples
The following code example uses the Where method to specify that the column value must be within the result set of a SQL subquery.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlWhereBuilderExamples
{
public class WhereExample
{
private readonly SchoolContext _context;
public WhereExample(SchoolContext dataContext)
{
// Sets data context.
_context = dataContext;
}
public void Example6()
{
// Declares SqlQueryBuilder.
ISqlQueryBuilder sqlQueryBuilder = new SqlQueryBuilder();
// Defines a subquery.
ISqlQueryBuilder deptSubQuery = new SqlQueryBuilder();
deptSubQuery.Select("DepartmentID")
.From("Department")
.Where("Name",
SqlBinaryOperator.In,
SqlBuilder.Parameter<string[]>("deptNames"));
// Defines the query parameter and specifies the columns to select.
var deptNames = new string[] { "Engineering", "English" };
string[] columns = { "CourseID", "Title" };
// Defines a SQL statement.
sqlQueryBuilder.Select(columns)
.From("Course")
.Where("DepartmentID", SqlBinaryOperator.In, deptSubQuery);
// Shows the SQL statement.
Console.WriteLine("The SQL statement: \n{0}",
sqlQueryBuilder.ToSqlString(_context));
// Executes the SQL statement.
var courses = _context.SqlExecutor
.Select<DynamicModel>(sqlQueryBuilder,
ParamValue.New("deptNames", deptNames));
Console.WriteLine();
Console.WriteLine("Title (first row) : {0}.",
courses[0].GetValue("Title"));
/*This code produces the following output:
The SQL statement:
SELECT
[CourseID],
[Title]
FROM [Course]
WHERE ([DepartmentID] IN(SELECT
[DepartmentID]
FROM [Department]
WHERE ([Name] IN(@deptNames) )))
Title (first row) : Chemistry.
*/
}
}
}
Applies to
.NET Standard
2.x