score:1

As @roughnex said in the comments, the following line:

``````l.Element("latitude") != null || l.Element("longitude") != null
``````

needs to be changed to:

``````l.Element("latitude") != null && l.Element("longitude") != null
``````

The reason for this is how boolean logic works.

In your code, you have the following conditional statement:

``````l.Element("latitude") != null || l.Element("longitude") != null
&& (Double)l.Element("latitude") >= southLat
&& (Double)l.Element("latitude") <= northLat
&& (Double)l.Element("longitude") >= westLong
&& (Double)l.Element("longitude") <= eastLong
``````

but let's cut out the details and make it simpler where each letter is a condition:

``````A || B && C && D && F
``````

When it's reduced to simpler terms, it is easier to see that if `A` resolves to `True`, then the rest of the conditional is ignored. That is why you were getting results that were outside of your desired area, because their latitude wasn't null.

score:0

For reference, this is a programming concept called short-circuiting. In boolean logic, if you have a condition

``````A || B
``````

and A evaluates to true, then B won't be evaluated because the expression (A || B) will be true regardless of B's value. Similarly, if

``````A && B
``````

and A evaluates to false, then B is not evaluated because the entire expression will evaluate to false, regardless of B's value. (False && true will always be false.)

Alternatively, if

``````A || B
``````

and A is false, then B must be evaluated, because in an OR comparison, only one has to be true for the whole expression to be true (False || True will be True). So since A is false, there's still a chance that B could be true, so we have to check.