SqlBuilder.Select(ISqlBuilder sqlBuilder, string alias = "") Method
.NET Standard 2.x
Creates an ISqlQueryBuilder
object for building a SQL SELECT statement and adds a column of SQL subquery (specified by an ISqlBuilder
object) to the select list of the SELECT clause. You can specify an alias for this column.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public static ISqlQueryBuilder Select(ISqlBuilder sqlBuilder, string alias = "");
Parameters
sqlBuilder
SnapObjects.Data.ISqlBuilder
An ISqlBuilder
object which represents a SQL subquery.
alias
System.String
(Optional) The alias of the column.
The default value is an empty string.
Returns
SnapObjects.Data.ISqlQueryBuilder
Returns a newly created ISqlQueryBuilder
object which can be used to build the SQL SELECT statement.
Examples
The following code example demonstrates how to use the Select(ISqlBuilder sqlBuilder, string alias = "") method. The column is selected using a subquery and is given an alias.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.SqlBuilderExamples
{
public class SelectExample
{
private SchoolContext _context;
public SelectExample(SchoolContext dataContext)
{
// Sets Data Context.
_context = dataContext;
}
public void Example3()
{
var sqlquerybuilder = new SqlQueryBuilder();
// Defines the subquery and sets the WHERE condition to associate with the tables.
sqlquerybuilder.Select("name")
.From("Department")
.Where("departmentid", "c.departmentid");
// Retrieves title values from the "course" table and name values from the "Department" table.
// Specifies "name" as the alias for the name column selected by the subquery.
var query = SqlBuilder
.Select(sqlquerybuilder, "name")
.Select("title")
.From("course", "c");
// Converts to raw SQL for the database corresponding to the data context.
string sql = query.ToSqlString(_context);
Console.WriteLine(sql);
/*This code example produces the following output:
SELECT
(SELECT
[name]
FROM [Department]
WHERE ([departmentid] = [c].[departmentid])) AS [name],
[title]
FROM [course] AS [c]
*/
}
}
}
Applies to
.NET Standard
2.x