ISqlGroupBuilder.GroupBy(params string[] groupExpressions) Method
.NET Standard 2.x
Creates a GROUP BY clause and adds an array of SQL expressions (each expression specifies a column or a non-aggregate calculation on a column).
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
ISqlGroupBuilder GroupBy(params string[] groupExpressions);
Parameters
groupExpressions
System.String[]
An array of SQL 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. To group by multiple columns, multiple parameters for column names 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 Example1()
{
var sqlQueryBuilder = new SqlQueryBuilder();
// Groups "StudentGrade" table by "StudentID" and "CourseID" columns (multiple parameters),
// and 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