ISqlHavingAndOr<TBuilder>.AndHaving(ISqlHavingCondition condition, string alias) Method
.NET Standard 2.x
Adds the AND
logical operator and a search condition (specified by an ISqlHavingCondition
object) to the HAVING
clause. You can specify an alias for the search condition.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
TBuilder AndHaving(ISqlHavingCondition condition, string alias);
Parameters
condition
SnapObjects.Data.ISqlHavingCondition
An ISqlHavingCondition
object which represents a search condition in the HAVING clause.
This object can be created by using the SqlBuilder.Having
method and so on.
alias
System.String
An alias for the search condition.
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 AndHaving method to add an AND condition to the HAVING clause.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlHavingAndOrExamples
{
public class AndHavingExample
{
private readonly SchoolContext _context;
public AndHavingExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example8()
{
ISqlQueryBuilder sqlQueryBuilder = new SqlQueryBuilder();
// Defines a HAVING condition.
ISqlHavingCondition having =
SqlBuilder.HavingRaw("StudentID < 10");
// Groups "StudentGrade" table by StudentID and CourseID, and gets the sum of Grade of each group.
// Specifies the HAVING condition for group as "StudentID > 0" and adds the condition defined above ("StudentID < 10").
// Gives the AndHaving condition a name so it can be modified or removed easily.
sqlQueryBuilder
.Select("StudentID")
.Select("sum(Grade)")
.From("StudentGrade")
.GroupBy("StudentID, CourseID")
.HavingRaw("StudentID > 0")
.AndHaving(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] > 0
AND ([StudentID] < 10))
*/
}
}
}
Applies to
.NET Standard
2.x