SqlBuilder.EmbeddedHaving(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter) Method
.NET Standard 2.x
Creates an ISqlHavingCondition
object which represents a search condition that can be used (enclosed in parentheses) when building the HAVING clause. Specifies a SQL expression on the left of the operator and a SQL parameter on the right of the operator.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public static ISqlHavingCondition EmbeddedHaving(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.
It can be created by calling the SqlBuilder.Parameter method.
Returns
SnapObjects.Data.ISqlHavingCondition
Returns the ISqlHavingCondition
object which can be used to add more search conditions to the HAVING clause.
Remarks
The newly created search condition is enclosed in parentheses. If the returned ISqlHavingCondition
object is used to add another search condition, both of these search conditions will be enclosed in the same pair of parentheses.
Examples
The following code example demonstrates how to use the EmbeddedHaving(string left, SqlOperator sqlOperator, ISqlParam parameter) method to specify the HAVING condition.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.SqlBuilderExamples
{
public class EmbeddedHavingExample
{
private SchoolContext _context;
public EmbeddedHavingExample(SchoolContext dataContext)
{
// Sets Data Context.
_context = dataContext;
}
public void Example4()
{
var sqlQueryBuilder = new SqlQueryBuilder();
// Defines an EmbeddedHaving condition StudentID = @studentid
var having = SqlBuilder
.EmbeddedHaving("StudentID",
SqlBinaryOperator.Equals,
SqlBuilder.Parameter<int>("studentid"));
// Builds a SQL statement to retrieve StudentID and sum(Grade) from the "StudentGrade" table, and
// group records by StudentID and CourseID which meet the condition: StudentID = @studentid.
var havingQuery = sqlQueryBuilder
.Select("StudentID")
.Select("sum(Grade)")
.From("StudentGrade")
.GroupBy("StudentID, CourseID")
.Having(having);
// Converts to raw SQL for the database corresponding to the data context.
string sql = havingQuery.ToSqlString(_context);
Console.WriteLine(sql);
/*This code produces the following output:
SELECT
[StudentID],
sum(Grade)
FROM [StudentGrade]
GROUP BY
[StudentID],
[CourseID]
HAVING ([StudentID] = @studentid)
*/
}
}
}
Applies to
.NET Standard
2.x