多个表中具有相同名称的列导致 SubSonic Select 出现问题

发布于 2024-07-25 14:36:23 字数 941 浏览 4 评论 0原文

还有其他问题(至少有 2 个我见过)与此类似,但我无法使用它们解决此问题。

现在的问题是:我有 3 个表,我只需要从中选择 4 列。 我正在使用 InnerJoin,它工作得很好。 当我向此选择添加“位置”时,问题就开始了。 我在两个表中有一个名为“Name”的列。 如果我简单地添加它

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

说“...不明确的列名..”

如果我使用完全限定的列名(表以列名为前缀)它说必须声明参数@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%");

我也尝试传递硬编码字符串但没有任何作用!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你げ笑在眉眼 2024-08-01 14:36:24

如果您将列对象传递给Where语句,SubSonic将使用它的完全限定名称而不是构建字符串。 您可以在对象上找到作为静态属性的列,因此在这种情况下,如果您有一个名为“TableOne”的对象,您可以使用“TableOne.NameColumn”并将其传递到Where():

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

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%");
凌乱心跳 2024-08-01 14:36:24

以下查询是否有效,我假设您使用的是 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%");

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%");
对风讲故事 2024-08-01 14:36:24

我从未使用过它,

但是您是否尝试将最后一行更改为:

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

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%');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文