ISqlHavingAndOr<TBuilder>.OrHavingRaw(string clause, params ISqlParameter[] parameters) Method
.NET Standard 2.x
Adds the OR
logical operator and a raw SQL to the HAVING
clause.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
TBuilder OrHavingRaw(string clause, params ISqlParameter[] parameters);
Parameters
clause
System.String
A string of raw SQL to be added to the HAVING
clause.
parameters
SnapObjects.Data.ISqlParameter[]
(Optional) An array of ISqlParameter
objects which define the SQL parameters used in the raw SQL.
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 OrHavingRaw method to add an OR condition (in raw SQL) to the HAVING clause.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlHavingAndOrExamples
{
public class OrHavingRawExample
{
private readonly SchoolContext _context;
public OrHavingRawExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example1()
{
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 as "StudentID = 6" or StudentID equal to the input parameter.
// The value of input parameter is specified when SQL is executed.
sqlQueryBuilder
.Select("StudentID")
.Select("sum(Grade)")
.From("StudentGrade")
.GroupBy("StudentID, CourseID")
.HavingRaw("StudentID = 6")
.OrHavingRaw("StudentID = @studentid",
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 "8" for the input parameter.
var studentGrade = _context.SqlExecutor.Select<DynamicModel>(sql, 8);
/*This code example produces the following output:
SELECT
[StudentID],
sum(Grade)
FROM [StudentGrade]
GROUP BY
[StudentID],
[CourseID]
HAVING ([StudentID] = 6
OR [StudentID] = @studentid)
*/
}
}
}
Applies to
.NET Standard
2.x