IQueryAndOrBuilder<TModel>.AndWhere(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter) Method
.NET Standard 2.x
Adds the AND
logical operator and a search condition to the WHERE clause; and specifies a SQL expression to the left of the operator and a SQL parameter to the right of the operator.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
IQueryAndOrBuilder<TModel> AndWhere(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter);
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.
parameter
SnapObjects.Data.ISqlParameter
An ISqlParameter
object which represents a SQL parameter on the right of the operator.
It can be created by calling the SqlBuilder.Parameter method.
Returns
Returns an IQueryAndOrBuilder<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 will have a dynamic value which is specified by an input parameter.
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> Example2()
{
// Get a QueryBuilder.
var Builder = _context.SqlModelMapper.GetQueryBuilder<Person>();
// adds AND condition to WHERE clause: DepartmentID > @id (a parameter)
// and DepartmentID > @id2 (a parameter).
Builder.Where("PersonID", SqlBinaryOperator.LessThan,
SqlBuilder.Parameter<int>("id"))
.AndWhere("PersonID", SqlBinaryOperator.GreaterThan,
SqlBuilder.Parameter<int>("id2"));
var result = (await Builder.LoadAsync(50, 20)).ToList();
var person = result.FirstOrDefault();
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);
Console.WriteLine();
Console.WriteLine("Result Count: {0}", result.Count);
return result.Count;
/*This code produces the following output:
PersonID: 21
FirstName: Roger
LastName: Holt
HireDate:
Discriminator: Student
Result Count: 14
*/
}
}
}
Example Refer To
Model Class: Person
Applies to
.NET Standard
2.x