ISqlGroupBuilder.GroupBy(string groupClause) Method
.NET Standard 2.x
Creates a GROUP BY clause, and adds a raw SQL string containing one or more column expressions (each expression specifies a column or a non-aggregate calculation on a column).
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
ISqlGroupBuilder GroupBy(string groupClause);
Parameters
groupClause
System.String
A raw SQL string containing one or more column expressions (each expression specifies a column or a non-aggregate calculation on a column).
Returns
SnapObjects.Data.ISqlGroupBuilder
Returns the current ISqlGroupBuilder
object.
Examples
The following code example demonstrates how to group by columns. When grouping by multiple columns, one parameter which contains columns (separated by commas) can be passed.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlGroupBuilderExamples
{
public class GroupByExample
{
private readonly SchoolContext _context;
public GroupByExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example2()
{
var sqlQueryBuilder = new SqlQueryBuilder();
// Groups "StudentGrade" table by "StudentID" and "CourseID" columns (one parameter with columns separated by commas).
// Gets the sum of Grade of each group.
sqlQueryBuilder
.Select("StudentID")
.Select("sum(Grade)")
.From("StudentGrade")
.GroupBy("StudentID, CourseID");
// 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]
*/
}
}
}
Applies to
.NET Standard
2.x