ISqlHavingBuilder.HavingRaw(string havingClause, string havingName, params ISqlParameter[] parameters) Method
.NET Standard 2.x
Creates a HAVING
clause, and adds a raw SQL to the HAVING
clause and specifies a name for the raw SQL.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
ISqlHavingAndOr HavingRaw(string havingClause, string havingName, params ISqlParameter[] parameters);
Parameters
havingClause
System.String
A string of raw SQL to be added to the HAVING
clause.
havingName
System.String
The name for the raw SQL.
parameters
SnapObjects.Data.ISqlParameter[]
(Optional) An array of ISqlParameter
objects which define the SQL parameters used in the raw SQL.
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 HavingRaw method to specify the HAVING condition (in raw SQL) for groups.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlHavingBuilderExamples
{
public class HavingRawExample
{
private readonly SchoolContext _context;
public HavingRawExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example2()
{
var sqlQueryBuilder = new SqlQueryBuilder();
// Groups "StudentGrade" table by StudentID and CourseID, and gets the sum of Grade of each group.
// Specifies the HAVING condition for group to: StudentID smaller than the input parameter.
// The value of input parameter is specified when SQL is executed.
// Specifies a name for the HAVING raw SQL so it can be modified or removed easily.
sqlQueryBuilder
.Select("StudentID")
.Select("sum(Grade)")
.From("StudentGrade")
.GroupBy("StudentID, CourseID")
.HavingRaw("StudentID < @studentid", "having",
SqlBuilder.Parameter<int>("studentid"));
// Converts to raw SQL for the database corresponding to the data context.
string sql = sqlQueryBuilder.ToSqlString(_context);
Console.WriteLine(sql);
// Executes the SQL and specifies a value for the input parameter.
var studentGrade = _context.SqlExecutor.Select<DynamicModel>(sql, 10);
/*This code example produces the following output:
SELECT
[StudentID],
sum(Grade)
FROM [StudentGrade]
GROUP BY
[StudentID],
[CourseID]
HAVING ([StudentID] < @studentid)
*/
}
}
}
Applies to
.NET Standard
2.x