Show / Hide Table of Contents

    ISqlQueryBuilder.Union(string sqlRaw) Method

    .NET Standard 2.x

    Uses UNION to combine the result set of the current SQL SELECT statement with the result set of another raw SQL SELECT statement.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      ISqlQueryBuilder Union(string sqlRaw);
    

    Parameters

    sqlRaw System.String

    The raw SQL SELECT whose result set is to be combined with the result set of the current SQL query.

    Returns

    SnapObjects.Data.ISqlQueryBuilder

    Returns the current ISqlQueryBuilder object.

    Examples

    The following code example demonstrates how to use the Union(string sqlRaw) method to merge the result sets of two or more SQL SELECT statements; it sorts the records in default order and does not include duplicate rows. These SELECT statements must have the same number of columns of the same data type and in the same selected order.

    using SnapObjects.Data;
    using System;
    
    namespace Appeon.ApiDoc.ISqlQueryBuilderExamples
    {
        public class UnionExample
        {
            private SchoolContext _context;
    
            public UnionExample(SchoolContext dataContext)
            {
                // Sets Data Context.
                _context = dataContext;
            }
    
            public void Example3()
            {
    
                // Declares SqlQueryBuilder. 
                var sqlbuilder = new SqlQueryBuilder();
    
                sqlbuilder.Select("courseid", "courseid")
                    .Select("url", "address")
                    .From("OnlineCourse", "online");
    
                // The SELECT statement to be merged with is a raw SQL string. 
                // Make sure the column number, data type, and the selected order are the same for these statements.
                sqlbuilder.Union("select courseid as courseid, location as address" +
                    " from OnsiteCourse");
    
                // Converts to raw SQL for the database corresponding to the data context.
                string sql = sqlbuilder.ToSqlString(_context);
    
                Console.WriteLine(sql);
                
                /*This code produces the following output:
                
                SELECT
                [courseid] AS [courseid],
                [url] AS [address]
                FROM [OnlineCourse] AS [online]
                UNION (SELECT
                [courseid] AS [courseid],
                [location] AS [address]
                FROM [OnsiteCourse])
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon