USER_8675309 USER_8675309 - 2 months ago 15
Vb.net 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 entitity.name
Where a.id = id _
AndAlso a.date < currentDate _
AndAlso a.statusId = 1 Or a.statusId = 3
Select


On line 4, will the
or
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

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

from:

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

to:

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