markrendle/Simple.Data — Issue #102: OrderBy fails if multiple tables have the same column name
Author: grumpydev
Created: 2011-10-21 10:52:49+00:00
State: open

I have two tables with some denormalized data in them (in this case "GeneratedDate"), I'm trying to do:

.OrderBy(db.GeneratedKeys.DateGenerated)

But that just gets added as "ORDER BY [DateGenerated]", which blows up with an ambiguous column name error.

I can see the code causing the issue in Simple.Data.Ado.ToOrderByDirective:

private string ToOrderByDirective(OrderByClause item)
{
var col = _table.FindColumn(item.Reference.GetName());
var direction = item.Direction == OrderByDirection.Descending ? " DESC" : string.Empty;
return col.QuotedName + direction;
}

I could change that last line to "return _table.QuotedName + "." + col.QuotedName" but that's:

a) Horrible
b) Probably very SQL specific

Perhaps Column can gain a "FullyQualifiedQuotedName" method that does this at a provider specific level?