ISqlQueryBuilder.UnionAll(ISqlQueryBuilder queryBuilder, string builderAlias) Method
.NET Standard 2.x
Uses UNION ALL
to combine the result set of the current SQL SELECT statement and the result set of another SQL SELECT statement generated by the ISqlQueryBuilder
object (the combined result set includes duplicate records); and specifies an alias for the ISqlQueryBuilder
object.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
ISqlQueryBuilder UnionAll(ISqlQueryBuilder queryBuilder, string builderAlias);
Parameters
queryBuilder
SnapObjects.Data.ISqlQueryBuilder
The ISqlQueryBuilder
object which represents a SQL query whose result set is to be combined with the current SQL query.
builderAlias
System.String
The alias of the ISqlQueryBuilder
object.
Returns
SnapObjects.Data.ISqlQueryBuilder
Returns the current ISqlQueryBuilder
object.
Remarks
The result sets to be combined must have the same number of columns and the same data type of these columns, and the order of these columns in the SELECT statement must be the same.
Examples
The following code example demonstrates how to use the UnionAll(ISqlQueryBuilder queryBuilder, string builderAlias) method to merge the result sets of two or more SQL SELECT statements and set an alias for it; it includes duplicate rows and does not sort the data. 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 UnionAllExample
{
private SchoolContext _context;
public UnionAllExample(SchoolContext dataContext)
{
// Sets Data Context.
_context = dataContext;
}
public void Example2()
{
// Declares SqlQueryBuilder.
var sqlbuilder1 = new SqlQueryBuilder();
var sqlbuilder2 = new SqlQueryBuilder();
sqlbuilder1.Select("courseid", "courseid")
.Select("location", "address")
.From("OnsiteCourse", "onsite");
sqlbuilder2.Select("courseid", "courseid")
.Select("url", "address")
.From("OnlineCourse", "online");
// Merges statement of sqlbuilder2 with sqlbuilder1, and specifies "test" as its alias.
sqlbuilder2.UnionAll(sqlbuilder1, "test");
// Converts to raw SQL for the database corresponding to the data context.
string sql = sqlbuilder2.ToSqlString(_context);
Console.WriteLine(sql);
/*This code produces the following output:
SELECT
[courseid] AS [courseid],
[url] AS [address]
FROM [OnlineCourse] AS [online]
UNION ALL
(SELECT
[courseid] AS [courseid],
[location] AS [address]
FROM [OnsiteCourse] AS [onsite])
*/
}
}
}
Applies to
.NET Standard
2.x