Active Query Builder support area

Enumeration of output columns in a (sub)query

Last modified:


The UnionSubQuery.QueryColumnsList collection contains information about SQL query expressions, their properties, and criteria. Each element of this collection has all the necessary properties to read and modify the text of expression, and its alias, ordering, grouping, aggregate function and criteria for this expression. The QueryColumnListItem.Select property determines whether an expression is listed in the SELECT list of output expressions.
The ExpressionField and ExpressionDatasource properties refer to the MetadataField and Datasource objects if the expression is a single database object field.
Below are the fragments of the QueryStructureDemo project that included in the installation package. You may review it for more details.
public void DumpSelectedExpressionsInfoFromUnionSubQuery(StringBuilder stringBuilder, UnionSubQuery unionSubQuery)
{
    // get list of QueryColumnListItems
    QueryColumnList columnsList = unionSubQuery.QueryColumnList;

    // dump all items
    for (int i = 0; i < columnsList.Count; i++)
    {
        QueryColumnListItem columnItem = columnsList[i];

        // only items with the Select property set to True go to SELECT list
        if (!columnItem.Select)
        {
            continue;
        }
        DumpSelectedExpressionInfo(stringBuilder, columnItem);
    }
}

private void DumpSelectedExpressionInfo(StringBuilder stringBuilder, QueryColumnListItem selectedExpression)
{
    // write full sql fragment of selected expression
    stringBuilder.AppendLine(selectedExpression.ExpressionString);

    // write alias
    if (!String.IsNullOrEmpty(selectedExpression.AliasString))
    {
        stringBuilder.AppendLine("  alias: " + selectedExpression.AliasString);
    }

    // write datasource reference (if any)
    if (selectedExpression.ExpressionDatasource != null)
    {
        stringBuilder.AppendLine("  datasource: " + selectedExpression.ExpressionDatasource.GetResultSQL());
    }

    // write metadata information (if any)
    if (selectedExpression.ExpressionField != null)
    {
        MetadataField field = selectedExpression.ExpressionField;
        stringBuilder.AppendLine("  field name: " + field.NameStr);
        string s = Enum.GetName(typeof(DbType), field.FieldType);
        stringBuilder.AppendLine("  field type: " + s);
    }
}

Is this article helpful for you?