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:

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?

Please note I m using .net 2.0 and subsonic 2.2

Thank you in advance.

Even do, Subsonic rules!

问题回答

You need to use the Constraint Expressions: WhereExpression, AndExpression, OrExpression, EndExpression.

Anything following “WhereExpression” (or Or/AndExpression) will be wrapped in parentheses. 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.

Also see: Using Nested 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. 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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签