ISqlHavingAndOr<TBuilder>.OrHaving(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder) Method
.NET Standard 2.x
Adds the OR
logical operator and a search condition to the HAVING
clause. Specifies a SQL expression on the left of the operator and a SQL subquery on the right of the operator.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
TBuilder OrHaving(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
TBuilder
Returns the TBuilder
object which can be used to add more search conditions to the current HAVING
clause.
Examples
The following code example demonstrates how to use the OrHaving method to add an OR condition to the HAVING clause.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlHavingAndOrExamples
{
public class OrHavingExample
{
private readonly SchoolContext _context;
public OrHavingExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example6()
{
var sqlQueryBuilder = new SqlQueryBuilder();
// Sets a query to get the largest Person ID.
var sqlBuilder = new SqlQueryBuilder();
sqlBuilder.SelectRaw("max(PersonID)")
.From("Person");
// Groups "StudentGrade" table by StudentID and CourseID, and gets the sum of Grade of each group.
// Specifies the HAVING condition for group as "CourseID > 100", or
// CourseID greater than the return value of the subquery.
sqlQueryBuilder
.Select("StudentID")
.Select("sum(Grade)")
.From("StudentGrade")
.GroupBy("StudentID, CourseID")
.HavingRaw("StudentID > 100")
.OrHaving("StudentID", SqlBinaryOperator.GreaterThan, sqlBuilder);
// Converts to raw SQL for the database corresponding to the data context.
string sql = sqlQueryBuilder.ToSqlString(_context);
Console.WriteLine(sql);
/*This code example produces the following output:
SELECT
[StudentID],
sum(Grade)
FROM [StudentGrade]
GROUP BY
[StudentID],
[CourseID]
HAVING ([StudentID] > 100
OR [StudentID] < (SELECT
MAX([PersonID])
FROM [Person]))
*/
}
}
}
Applies to
.NET Standard
2.x