Show / Hide Table of Contents

    ISqlHavingBuilder.AsOrderBy() Method

    .NET Standard 2.x

    Returns an ISqlOrderThenBuilder object which can be used to add sorting criteria to the ORDER clause.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      ISqlOrderThenBuilder AsOrderBy();
    

    Returns

    SnapObjects.Data.ISqlOrderThenBuilder

    Returns an ISqlOrderThenBuilder object which can be used to add sorting criteria to the ORDER clause.

    Examples

    The following code example demonstrates how to use the AsOrderBy method to get the existing OrderBy condition and add a condition to it.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlHavingBuilderExamples
    {
        public class AsOrderByExample
        {
            private readonly SchoolContext _context;
    
            public AsOrderByExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var sqlQueryBuilder = new SqlQueryBuilder();
    
                // Groups "StudentGrade" table by StudentID and CourseID, and gets the sum of Grade of each group.    
                // Sets to sort by StudentID in ascending order.      
                sqlQueryBuilder
                    .Select("StudentID")
                    .Select("sum(Grade)")
                    .From("StudentGrade")
                    .GroupBy("StudentID, CourseID")
                    .OrderByAscending("StudentID");
    
                // Gets the current OrderBy condition and adds another condition: sort by CourseID in ascending order.        
                sqlQueryBuilder.AsOrderBy()
                               .ThenByAscending("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]
                    ORDER BY
                        [StudentID] ASC,
                        [CourseID] ASC
               */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon