简体   繁体   English

亚音速查询:产生的查询问题

[英]Subsonic query: issue with the produced query

I'm having a problem with subsonic query. 亚音速查询出现问题。 The deal is, I have a view and I want to query it's data to produce something like the following SQL statement: 关键是,我有一个视图,我想查询它的数据以产生类似以下SQL语句的内容:

select * 
from myView
where (col1 like '%a%' or col2 like '%a%' or col3 like '%a%') 
  and col4 = 1 and col5 = 2

But instead the query that is submited to the DB is something like this: 但是,提交给数据库的查询是这样的:

select * 
from myView
where col1 like '%a%' or col2 like '%a%' or col3 like '%a%' 
  and col4 = 1 and col5 = 2

Is there a way to do something like the fisrt query? 有没有办法做像fisrt查询一样的事情?

Please note I'm using .net 2.0 and subsonic 2.2 请注意,我正在使用.net 2.0和subsonic 2.2

Thank you in advance. 先感谢您。

Even do, Subsonic rules! 即使这样做,亚音速规则!

You need to use the Constraint Expressions : WhereExpression , AndExpression , OrExpression , EndExpression . 您需要使用约束表达式WhereExpressionAndExpressionOrExpressionEndExpression

Anything following “WhereExpression” (or Or/AndExpression) will be wrapped in parentheses. “ WhereExpression”(或“ Or / AndExpression”)之后的所有内容都将用括号括起来。 You can close the expression by using “CloseExpression()”, or it will be closed for you if another is started (as with OrExpression above) or if the query ends. 您可以使用“ CloseExpression()”关闭表达式,或者如果另一个表达式已启动(如上述OrExpression)或查询结束,它将为您关闭。

Also see: Using Nested Where/And/Or . 另请参阅: 使用嵌套的Where / And / Or

If you post your current code maybe I will be able to give more specific answer but basically you have to start WhereExpression . 如果您发布当前代码,也许我可以给出更具体的答案,但是基本上您必须启动WhereExpression For example code like this: 例如这样的代码:

var usersQuery = new Select(new SubSonic.TableSchema.TableColumn[] { User.UserIdColumn, User.NameColumn })
            .From(User.Schema)            
            .WhereExpression(User.UserIdColumn.ColumnName).IsEqualTo(1).Or(User.UserIdColumn).IsEqualTo(2)
            .CloseExpression()
            .And(User.NameColumn).Like("a")

Gives query like: 给出如下查询:

SELECT [dbo].[Users].[UserId], [dbo].[Users].[Name]
 FROM [dbo].[Users]
 WHERE ([dbo].[Users].[UserId] = @UserId0 OR [dbo].[Users].[UserId] = @UserId)
 AND [dbo].[Users].[Name] LIKE @Name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM