USER_8675309 USER_8675309 - 1 year ago 164 Question

VB.NET LINQ query Where with AndAlso and Or

In VB.NET, if I am doing a LINQ query that looks like this:

From a in
Where = id _
AndAlso < currentDate _
AndAlso a.statusId = 1 Or a.statusId = 3

On line 4, will the
clause evaluate to return all entities with a statusId of 3, or will it only return the entities that fit the other criteria and have a statusId of 1 or 3?

If that line were changed to

AndAlso (a.statusId = 1 Or a.statusId = 3)

How would that change the expected results?

Answer Source

In VB.NET, conjunction (And, AndAlso) operators occur before inclusive disjunction (Or, OrElse) operators. See Operator Precedence in Visual Basic.

So as soon as a.statusId = 3 is true, it will return true.

And adding parenthesis

AndAlso (a.statusId = 1 Or a.statusId = 3)

will therefore change the behaviour


return TRUE if ( = id AND < currentDate AND a.statusId = 1) OR a.statusId = 3


return TRUE if = id AND < currentDate AND (a.statusId = 1 OR a.statusId = 3)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download