subsonic 2 连接多列

发布于 2024-08-31 21:38:30 字数 1137 浏览 2 评论 0原文

我想将以下语句转移到 SubSonic 2.2

    SELECT b.*
    FROM tableA a
      INNER JOIN tableB b
        ON (a.year = b.year AND a.month = b.monath AND a.userid = b.userid);

我的问题是 SubSonic 的 SqlQuery.LeftInnerJoin() 命令没有重载,需要多于一列。

由于任何连接只能使用 where 子句重写,因此我在 sql: 中执行了以下操作,

    SELECT b.*
    FROM tableA a, tableB b
    WHERE a.year = b.year
      AND a.month = b.month
      AND a.userid = b.userid

这应该提供相同的结果(事实上,至少对于 mysql 来说,这些语句在逻辑上绝对没有区别)。

但我在将其转换为亚音速时也遇到了困难,因为“IsEqualTo(...)”成员足够聪明,可以找出我的参数是一个字符串并将其放入引号中。

DB.Select("TableB.*")
  .From<TableA>()
  .From<TableB>()
  .Where(TableA.YearColumn).IsEqualTo("TableB.Year")
  .And(TableA.MonthColumn).IsEqualTo("TableB.Month")
  .And(TableA.UseridColumn).IsEqualTo("TableB.UserId")

(我尝试了不同的方法来设置 IsEqualTo 参数)

IsEqualTo(TableB.YearColumn)
IsEqualTo(TableB.YearColumn.QualifiedName)

该参数要么被解释为,

TableA.Year = 'TableB.Year'

要么得到 SqlQueryException。

有人可以告诉我如何用亚音速执行此查询(第一个 - 使用 JOIN 或第二个)?谢谢

I want to transfer the following statement to SubSonic 2.2

    SELECT b.*
    FROM tableA a
      INNER JOIN tableB b
        ON (a.year = b.year AND a.month = b.monath AND a.userid = b.userid);

My problem is that SubSonic's SqlQuery.LeftInnerJoin() command has no overload which takes more than one column.

Since any join can be rewritten only using where clauses, I did the following in my sql:

    SELECT b.*
    FROM tableA a, tableB b
    WHERE a.year = b.year
      AND a.month = b.month
      AND a.userid = b.userid

which should deliver the same result (in fact, at least for mysql, there is logically absolutely no difference between these statements).

But I also got stuck transfering this to subsonic because the "IsEqualTo(...)" member is smart enough to figure out that my parameter is a string and puts it into quotes.

DB.Select("TableB.*")
  .From<TableA>()
  .From<TableB>()
  .Where(TableA.YearColumn).IsEqualTo("TableB.Year")
  .And(TableA.MonthColumn).IsEqualTo("TableB.Month")
  .And(TableA.UseridColumn).IsEqualTo("TableB.UserId")

(I tried different ways in setting the IsEqualTo parameter)

IsEqualTo(TableB.YearColumn)
IsEqualTo(TableB.YearColumn.QualifiedName)

Either the parameter is interpreted as

TableA.Year = 'TableB.Year'

or I get a SqlQueryException.

Can somebody tell me how to do this query with subsonic (Either the first - with JOIN or the second one)? Thanks

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

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

发布评论

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

评论(1

堇色安年 2024-09-07 21:38:30

使用开箱即用的 SubSonic 2 就可以做到这一点。

也就是说,您有以下选择:

扩展 SubSonic

如果您已经熟悉 SubSonic,您可以考虑向 SubSonic 本身添加多列联接。

使用视图、存储过程、表函数

如果您不想弄乱 SubSonics 代码,请在 sql server 中使用视图、存储过程和/或表函数。 SubSonic 可以轻松地从视图和存储过程访问数据。

使用 InlineQuery

InlineQuery 允许您执行任何 sql - 如果它是选择在代码中包含裸sql。

使用 InlineQuery 的丑陋解决方法

如果您绝对想使用 SubSonic 创建查询,您可以尝试以下操作:

SqlQuery q = DB.Select()
  .From<TableA>()
  .CrossJoin<TableB>()
  .Where(TableA.YearColumn).IsEqualTo(0)
  .And(TableA.MonthColumn).IsEqualTo(0)
  .And(TableA.UseridColumn).IsEqualTo(0);

构建 SQL 语句,并替换参数名称:

string s = q.BuildSqlStatement();
s = s.Replace(q.Constraints[0].ParameterName, TableB.YearColumn.QualifiedName);
s = s.Replace(q.Constraints[1].ParameterName, TableB.MonthColumn.QualifiedName);
s = s.Replace(q.Constraints[2].ParameterName, TableB.UserIdColumn.QualifiedName);

然后将 s 与 InlineQuery 一起使用。

With SubSonic 2 out of the box you can't.

This said, you have the following alternatives:

Extend SubSonic

If you're already familiar with SubSonic, you may consider to add multi-column joins to SubSonic itself.

Use views, Stored procedures, table functions

If you do not want to mess with SubSonics code, use views, stored procedures and/or table functions within sql server. SubSonic makes it easy to access data from views and stored procedures.

Use an InlineQuery

InlineQuery allows you to execute any sql - if it is an option to have bare sql in your code.

Ugly workaround with InlineQuery

If you absolutely want to create your query with SubSonic, you can try this:

SqlQuery q = DB.Select()
  .From<TableA>()
  .CrossJoin<TableB>()
  .Where(TableA.YearColumn).IsEqualTo(0)
  .And(TableA.MonthColumn).IsEqualTo(0)
  .And(TableA.UseridColumn).IsEqualTo(0);

Build the SQL statement, and replace the parameter names:

string s = q.BuildSqlStatement();
s = s.Replace(q.Constraints[0].ParameterName, TableB.YearColumn.QualifiedName);
s = s.Replace(q.Constraints[1].ParameterName, TableB.MonthColumn.QualifiedName);
s = s.Replace(q.Constraints[2].ParameterName, TableB.UserIdColumn.QualifiedName);

Then use s with an InlineQuery.

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