I have this subsonic query:
Select.AllColumnsFrom<Data.Group>()
.Where(Data.Group.GroupIDColumn).IsNotEqualTo(m_sRootGroupID)
.AndExpression(Data.Group.Columns.OwnerPersonID).IsEqualTo(gUserID)
.OrExpression(Data.Group.Columns.OwnerPersonID).IsEqualTo(gUserContextID)
.AndExpression(Data.Group.Columns.IsCallList).IsEqualTo(true)
.CheckLogicalDelete().ExecuteTypedList<Groups>();
At first sight, it seems that the previous developer has mistaken AndExpression for And. I know that AndExpression encloses the preceding statements inside parentheses.
How is the above subsonic query be translated into SQL?
This is what I'm thinking:
SELECT *
FROM tblGroups
WHERE GroupID <> m_sRootGroupID
AND ( --first [AndExpression]
OwnerPersonID = `gUserID
OR ( -- [OrExpression]
OwnerPersonID = gUserContextID
AND ( -- second [AndExpression]
IsCallList = true
)
)
) -- first [AndExpression]
AND ISNULL(IsDeleted,0) = 0
How is the ..Expressions being handled if there is no CloseExpression?
As has been pointed out by @MikeWalsh in his answer, calling
.ToString()shows what the subsonic query looks like when translated to SQL.Thus, the above code can be translated into:
Which tells us that calling
AndExpression/OrExpressionwithout aCloseExpression, subsonic assumes there is only one condition inside theANDstatement enclosed in parentheses, thus:.AndExpression(Data.Group.Columns.OwnerPersonID).IsEqualTo(gUserID)is translated into:
AND ([dbo].[tblGroups].[OwnerPersonID] = @OwnerPersonID1)EDIT:
That's what I thought. However, when using this subsonic query:
I got this query (formatting mine):
As you can see, the first
AndExpressionincludes.Or(Data.Group.Columns.OwnerPersonID).IsEqualTo("SomeContextID")inside its parentheses, which implies that..Expression's continue to include preceding statements inside the parentheses until it finds another..Expression(as we can observe on the secondAndExpression.Adding additional
And/Ormethods after the firstAndExpressionstrengthens the proof that