ISqlWhereBuilder.Where(ISqlWhereCondition condition, string whereName) Method
.NET Standard 2.x
Creates a WHERE clause, adds a search condition to the WHERE clause by an ISqlWhereCondition
object, and specifies a name for the search condition.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
ISqlWhereAndOr Where(ISqlWhereCondition condition, string whereName);
Parameters
condition
SnapObjects.Data.ISqlWhereCondition
An ISqlWhereCondition
which represents a search condition in the WHERE Clause.
This object can be created by using the SqlBuilder.Where
method and so on.
whereName
System.String
The name for the search condition. It can be used to remove a search condition from the WHERE clause.
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 declares an ISqlWhereCondition object, uses it in the Where method, and specifies an alias for it.
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 Example8()
{
// Declares SqlQueryBuilder.
ISqlQueryBuilder sqlQueryBuilder = new SqlQueryBuilder();
// Defines a WHERE condition.
ISqlWhereCondition whereCondition =
SqlBuilder.Where("CourseId", SqlBuilder.Parameter<int>("id"));
// Defines a query parameter for the query.
int CourseId = 1050;
// Defines a SQL statement, specifies to use the WHERE condition defined above and specifies "where1" as its alias.
sqlQueryBuilder.SelectRaw("CourseID, Title")
.From("Course")
.Where(whereCondition, "where1");
Console.WriteLine("The SQL SELECT statement: \n {0}",
sqlQueryBuilder.ToSqlString(_context));
// Executes the SQL statement, and places the returned data into a model.
var courses = _context.SqlExecutor
.Select<DynamicModel>(sqlQueryBuilder, CourseId);
Console.WriteLine();
Console.WriteLine("Title (first row) {0}.",
courses[0].GetValue("Title"));
// Removes the WHERE condition.
sqlQueryBuilder.RemoveWhere("where1");
Console.WriteLine();
Console.WriteLine("After WhereCondtion removed: \n {0}",
sqlQueryBuilder.ToSqlString(_context));
/*This code produces the following output:
The SQL SELECT statement:
SELECT
[CourseID],
[Title]
FROM [Course]
WHERE ([CourseId] = @id)
Title (first row) Chemistry.
After WhereCondtion removed:
SELECT
[CourseID],
[Title]
FROM [Course]
*/
}
}
}
Applies to
.NET Standard
2.x