I'm using LLBLGen Pro 2.5 final (December 5th build) using SQL Server 2005 and .Net 2.0.
I'm going to code around this so that I don't need the data sorted, but I'd like to know for future reference:
If I use an EntityField object that uses an expression in a sort clause, it emits the name of the field, rather than the expression. This is in a prefetch, so DISTINCT is not emitted, so it shouldn't matter that the expression isn't one of the fields. And if it did, I'd expect an error since there is no field of that name. (Well, I'd expect it from LLBL, I'm getting exactly that from SQL.)
Shortening the names a bit, here's what I'm doing:
IExpression adExp = new DbFunctionCall("COALESCE", new object[] {
ATFields.ATDate, ATFields.CreatedDate
});
EntityField aDate = new EntityField("ADate", adExp);
...
sort = new SortExpression();
sort.Add(aDate | SortOperator.Descending);
Prefetch.Add(AccountEntity.PrefetchPathAT, 0, null, null, sort);
The SQL code emitted will contain
ORDER BY [ADate] DESC
rather than
ORDER BY COALESCE(blah.dbo.[ATDate], blah.dbo.[CreatedDate])
I know this won't work if DISTINCT is specified, but it isn't. In SQL Server at least, this can't ever work, as you can't reference a select list alias in the order by clause, you have to use the expression. (In this case the field isn't even in the select list, but even if it were this would fail.)
Is it simply a limitation that you can't sort on an expression?