Subsonic 3 简单查询内连接 SQL 语法

发布于 2024-07-30 03:19:52 字数 1327 浏览 5 评论 0原文

我想对两个表(BusinessUnit 和 UserBusinessUnit)执行简单的联接,以便获得分配给给定用户的所有 BusinessUnit 的列表。

第一次尝试有效,但没有覆盖 Select 允许我限制返回的列(我从两个表中获取所有列):

var db = new KensDB();
SqlQuery query = db.Select
 .From<BusinessUnit>()
 .InnerJoin<UserBusinessUnit>( BusinessUnitTable.IdColumn, UserBusinessUnitTable.BusinessUnitIdColumn )
 .Where( BusinessUnitTable.RecordStatusColumn ).IsEqualTo( 1 )
 .And( UserBusinessUnitTable.UserIdColumn ).IsEqualTo( userId );

第二次尝试允许列名限制,但生成的 sql 包含复数表名(?

SqlQuery query = new Select( new string[] { BusinessUnitTable.IdColumn, BusinessUnitTable.NameColumn } )
 .From<BusinessUnit>()
 .InnerJoin<UserBusinessUnit>( BusinessUnitTable.IdColumn, UserBusinessUnitTable.BusinessUnitIdColumn )
 .Where( BusinessUnitTable.RecordStatusColumn ).IsEqualTo( 1 )
 .And( UserBusinessUnitTable.UserIdColumn ).IsEqualTo( userId );

) ...

SELECT [BusinessUnits].[Id], [BusinessUnits].[Name]
 FROM [BusinessUnits]
 INNER JOIN [UserBusinessUnits]
 ON [BusinessUnits].[Id] = [UserBusinessUnits].[BusinessUnitId]
 WHERE [BusinessUnits].[RecordStatus] = @0
 AND [UserBusinessUnits].[UserId] = @1

所以,有两个问题:
- 如何限制方法 1 返回的列?
- 为什么方法 2 在生成的 SQL 中将列名复数化(我可以解决这个问题吗?)

我正在使用 3.0.0.3...

I want to perform a simple join on two tables (BusinessUnit and UserBusinessUnit), so I can get a list of all BusinessUnits allocated to a given user.

The first attempt works, but there's no override of Select which allows me to restrict the columns returned (I get all columns from both tables):

var db = new KensDB();
SqlQuery query = db.Select
 .From<BusinessUnit>()
 .InnerJoin<UserBusinessUnit>( BusinessUnitTable.IdColumn, UserBusinessUnitTable.BusinessUnitIdColumn )
 .Where( BusinessUnitTable.RecordStatusColumn ).IsEqualTo( 1 )
 .And( UserBusinessUnitTable.UserIdColumn ).IsEqualTo( userId );

The second attept allows the column name restriction, but the generated sql contains pluralised table names (?)

SqlQuery query = new Select( new string[] { BusinessUnitTable.IdColumn, BusinessUnitTable.NameColumn } )
 .From<BusinessUnit>()
 .InnerJoin<UserBusinessUnit>( BusinessUnitTable.IdColumn, UserBusinessUnitTable.BusinessUnitIdColumn )
 .Where( BusinessUnitTable.RecordStatusColumn ).IsEqualTo( 1 )
 .And( UserBusinessUnitTable.UserIdColumn ).IsEqualTo( userId );

Produces...

SELECT [BusinessUnits].[Id], [BusinessUnits].[Name]
 FROM [BusinessUnits]
 INNER JOIN [UserBusinessUnits]
 ON [BusinessUnits].[Id] = [UserBusinessUnits].[BusinessUnitId]
 WHERE [BusinessUnits].[RecordStatus] = @0
 AND [UserBusinessUnits].[UserId] = @1

So, two questions:
- How do I restrict the columns returned in method 1?
- Why does method 2 pluralise the column names in the generated SQL (and can I get round this?)

I'm using 3.0.0.3...

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

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

发布评论

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

评论(2

荭秂 2024-08-06 03:19:52

到目前为止,我对 3.0.0.3 的经验表明,尽管在版本 2 中,查询工具还无法做到这一点。

我认为版本 3 的首选方法(到目前为止)是使用 linq 查询,如下所示:

var busUnits = from b in BusinessUnit.All()
join u in UserBusinessUnit.All() on b.Id equals u.BusinessUnitId
select b;

So far my experience with 3.0.0.3 suggests that this is not possible yet with the query tool, although it is with version 2.

I think the preferred method (so far) with version 3 is to use a linq query with something like:

var busUnits = from b in BusinessUnit.All()
join u in UserBusinessUnit.All() on b.Id equals u.BusinessUnitId
select b;
庆幸我还是我 2024-08-06 03:19:52

我自己也遇到了复数表名,但这是因为我在进行架构更改后只重新运行一个模板。

一旦我重新运行所有模板,复数表名称就消失了。

尝试重新运行所有 4 个模板,看看是否能解决您的问题。

I ran into the pluralized table names myself, but it was because I'd only re-run one template after making schema changes.

Once I re-ran all the templates, the plural table names went away.

Try re-running all 4 templates and see if that solves it for you.

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