ISqlHavingBuilder.Having(ISqlHavingCondition condition, string havingName) Method
.NET Standard 2.x
Creates a HAVING
clause, and adds a search condition to the HAVING
clause by an ISqlHavingCondition
object and specifies the name for the search condition.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
ISqlHavingAndOr Having(ISqlHavingCondition condition, string havingName);
Parameters
condition
SnapObjects.Data.ISqlParameter
An ISqlHavingCondition
object which represents a search condition in the HAVING
clause.
This object can be created by using SqlBuilder.Having
method and so on.
havingName
System.String
The name for the search condition.
Returns
SnapObjects.Data.ISqlHavingAndOr
Returns the ISqlHavingAndOr
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 Having method to specify the HAVING condition for groups.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlHavingBuilderExamples
{
public class HavingExample
{
private readonly SchoolContext _context;
public HavingExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example8()
{
var sqlQueryBuilder = new SqlQueryBuilder();
// Defines a HAVING condition.
var having = SqlBuilder.HavingRaw("StudentID < 10");
// Groups "StudentGrade" table by StudentID and CourseID, and gets the sum of Grade of each group.
// The HAVING condition for groups is the condition defined above: StudentID < 10.
// Specifies a name for the HAVING conditon so it can be modified or removed easily.
sqlQueryBuilder
.Select("StudentID")
.Select("sum(Grade)")
.From("StudentGrade")
.GroupBy("StudentID, CourseID")
.Having(having, "having");
// 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] < 10)
*/
}
}
}
Applies to
.NET Standard
2.x