JaggenSWE
JaggenSWE

Reputation: 2094

LINQ query doesn't give any results (finding which interval a number belongs to)

I have a property class which looks like this

ZipStart | ZipEnd | Acode

10000    | 13999  | BEK

14000    | 14999  | ATE

Now, I have a value, for example 11332 and I want to look up which Acode that number belongs to, for this I've the following code:

           var res = from o in ListOfDataClass
                  where iCode >= o.ZipStart && o.ZipEnd <= iCode
                  select o;

            var c = res.FirstOrDefault();
            return c.Acode;

However this results in null from time to time, I haven't been able to find a pattern yet though, but since the issue arises in this method I've been able to deduct from the debugger that the iCode IS actually 11332 but that the LINQ-query doesn't yield any results.

Any ideas?

Upvotes: 0

Views: 79

Answers (2)

Dave
Dave

Reputation: 8461

I think you have to move your condition about a little:

where iCode >= o.ZipStart && iCode <=  o.ZipEnd

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

Your condition is incorrect.
The second part has to be o.ZipEnd >= iCode or iCode <= o.ZipEnd.

Upvotes: 5

Related Questions