Column with same name in multiple tables causing problem in SubSonic Select

三世轮回 提交于 2019-12-08 05:13:11

问题


there are other question (at least 2 I've seen them) similar to this but I'm not able to solve this using them.

Now the problem: I've 3 table from which I need to select 4 columns only. I'm using InnerJoin and it is working perfectly. Problem starts when I add a Where to this Select. I've a column named "Name" in two tables. If I add simply the

.Where("Name").Like("A%")

It says "... ambiguous column name.."

If I use fully qualified column name (with table prefixed to column name) it says must declare parameter @TABLE_NAME

  SqlQuery sq = new Select(Tables.TableOne + "." + TableOne.Columns.MemberId + 
  " AS MemberId",
  Tables.TableTwo + "." + TableTwo.Columns.Name + " AS MemberName",
  Tables.TableOne + "." + TableOne.Columns.ExpiryOn + " AS MembershipExpiresOn",
  Tables.TableFour + "." + TableFour.Columns.Name + " AS Country")
  .From(DAL.Tables.TableOne)
  .InnerJoin(Tables.TableTwo)
  .InnerJoin(Tables.TableThree)
  .InnerJoin(Tables.TableFour, TableFour.Columns.CountryCode,
  Tables.TableThree, TableThree.Columns.CountryOfBirth).
  sq.Where(Tables.TableTwo + "." + TableTwo.Columns.Name).Like("A%");

I've tried to pass hard-coded string also but nothing works!


回答1:


If you pass in the column object to the Where statement SubSonic will use it's fully qualified name instead of building a string. You can find the column on the object as a static property, so in this case if you have an object called "TableOne" you can use "TableOne.NameColumn" and pass that into the Where():

...
sq.Where(TableTwo.NameColumn).Like("A%");



回答2:


Does the following query work, I'm assuming you're using 2.2:

SqlQuery sq = new Select(TableOne.Columns.MemberId + " AS MemberId",
    TableTwo.Columns.Name + " AS MemberName",
    TableOne.Columns.ExpiryOn + " AS MembershipExpiresOn",
    TableFour.Columns.Name + " AS Country")
  .From(TableOne.Schema)
  .InnerJoin(TableTwo.Schema)
  .InnerJoin(TableThree.Schema)
  .InnerJoin(TableFour.Schema)
  .Where(TableTwo.Columns.Name).Like("A%");



回答3:


I haven't used it ever,

but have your tried to change your last line to:

sq.WhereExpression(Tables.TableTwo + "." + TableTwo.Columns.Name + " LIKE 'A%');


来源:https://stackoverflow.com/questions/1061851/column-with-same-name-in-multiple-tables-causing-problem-in-subsonic-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!