That thread was not one of my bests... I made several mistakes in one thread...
(the codebase is so big I don't remember every construct from my bare head anymore, and I should have looked it up before suggesting something).
The Order By clauses are generated in the DQE, in a loop. The loop constructs a fieldname and appends the operator. The thought I had was, (before checking it) that ISortClause could produce that fieldname without a problem. However that is not the case, ISortClause only contains data which is used by the database specific generator object to produce a name, and in the case of SqlServer, it always produces [table].[fieldname] or similar names, so that's not it.
However, what CAN be done, and it's quite simple, is this. As the OrderBy routine looks like this:
/// <summary>
/// Appends an ORDER BY clause to the query specified.
/// </summary>
/// <param name="queryText">query text currently being build</param>
/// <param name="sortClauses">sort clauses collection</param>
/// <param name="creator">Creator for sqlserver specific sql</param>
private static void AppendOrderByClause(StringBuilder queryText, ISortExpression sortClauses, IDbSpecificCreator creator)
{
// append ORDER BY plus the sort clauses
queryText.Append(" ORDER BY ");
for(int i = 0; i < sortClauses.Count; i++)
{
ISortClause sortClauseToUse = sortClauses[i];
if(i > 0)
{
queryText.Append(",");
}
string fieldName = sortClauseToUse.FieldToSortCore.Alias;
if((sortClauseToUse.FieldToSortCore.ExpressionToApply==null) && (sortClauseToUse.FieldToSortCore.AggregateFunctionToApply==AggregateFunction.None))
{
fieldName = creator.CreateFieldName(sortClauseToUse.PersistenceInfo, sortClauseToUse.FieldToSortCore.Alias, sortClauseToUse.ObjectAlias, false);
}
queryText.AppendFormat("{0} {1}", fieldName, creator.ConvertSortOperator(sortClauseToUse.SortOperatorToUse));
}
}
you see that the fieldName is the alias specified in the field if an expression and/or aggregate function is specified with the field (because the expression is in the select list etc.)
So to mislead this routine, you could try: (haven't tested this, but looking at this routine THIS TIME it should work
)
Below is for selfservicing, if you're using adapter, let me know.
// create empty field object, doesn't matter
IEntityField newIDField = new EntityField();
newIDField.Alias = "NEWID()";
newIDField.ExpressionToApply = new Expression();
ISortExpression sorter = new SortExpression(new SortClause(newIDField, SortOperator.Ascending));
As ExpressionToApply is not null, it will pick the Alias for the fieldname and therefore should result in ORDER BY NEWID() ASC