markrendle / Simple.Data

A light-weight, dynamic data access component for C# 4.0
MIT License
1.33k stars 303 forks source link

OrderBy issue with duplicate table names in different schemas and no aliases #359

Open Jevonius opened 9 years ago

Jevonius commented 9 years ago

Hi,

I've noticed a problem with OrderBy that (based on some brief spelunking) I've narrowed down to ToOrderByDirective. I've put together a quick hack fix, but haven't submitted it as a PR as it's quite, well, hacky. I think the problem boils down to the code not handling/considering a lack of aliases in queries.

Example: Two tables, both called Location in different schemas (let's say Hotel and Client). A query such as db.Hotel.Location.All().OrderByName() fails with a Sequence contains more than one element exception. This is because there are no aliases in use, so the code in ToOrderByDirective falls to the final else block, which tries to FindTable across all the schemas.

Hack Fix: Adding an extra else block along the lines of if( _columns.Any( r => ( r.ToString() == item.Reference.ToString() ) ) ) { name = item.Reference.GetAllObjectNamesDotted(); } does resolve the issue, but I suspect there's a much nicer (and better/correct) way to achieve the same thing.

Secondary issue: While trying to identify the issue I tried the alternative syntax of .OrderBy( db.Hotel.Name ) but get an argument exception OrderByColumn form does not accept parameters - is the documentation for OrderBy out of date, or should this be working?

markrendle commented 9 years ago

Hi Jevon,

The whole issue of schema is something that I'm looking at right now in the v2 work. v1 is basically using guess-work; I want to make it more explicit, just need to come up with the right syntax.

The secondary issue sounds like a bug; I'll take a look.