1

I need to check if a postcode is within a given start and end postcode, using linq.

Here is what I have so far but it's not right at all, can someone point me in the right direction?

List<DestinationStation> stations = DestinationStation.GetDestinationStations();
var query = from s in stations
            where postcode <= s.FromPostcode && postcode >= s.ToPostcode
            select s;
Console.WriteLine(query.ToList());
svick
  • 236,525
  • 50
  • 385
  • 514
Carl Weis
  • 6,794
  • 15
  • 63
  • 86
  • 1
    Usually postcode is in string format to cater for '0213'. So just checking what is the data value of post code and can you give example? Sometimes postcode contains alphanumeric – ysrb Nov 03 '11 at 01:14
  • 3
    You say you want to check *a* postcode but your code seems to check in what ranges one postcode belongs. What exactly do you want? What's not right about your current code? What data type is your postcode and what are the possible values? Are you taking about US ZIP codes or something else? – svick Nov 03 '11 at 01:15
  • Postcode is in UK format TW4 6JS I have a range of postcodes and I need to see if a postcode I pass in, falls within the fromPostcode and toPostCode. – Carl Weis Nov 03 '11 at 01:23
  • So you have one range? Why does your code have a list of ranges then? And, again, what's wrong with your current code? – svick Nov 03 '11 at 01:32
  • I have an List which each DestinationStation object has a FromPostcode and a ToPostcode, they are strings. I need to check if a given postcode is within any of the FromPostcodes and ToPostcodes for a given DestinationStation object...Make sense? – Carl Weis Nov 03 '11 at 01:34

4 Answers4

2

Try CompareTo for strings. Does this work?

var query =
    from s in stations
    where postcode.CompareTo(s.FromPostcode) >= 0
    where postcode.CompareTo(s.ToPostcode) <= 1
    select s;
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
1

I've assumed that the natural ordering of strings is what you mean by "between". If that is not true, you should look at the IComparable interface to have more control over ordering.

I've also made the comparisons exclusive. You can change the operators to make them inclusive, though.

    class Program
{
    static void Main(string[] args)
    {
        var postcode = "B";
        var stations = DestinationStation.GetDestinationStations();
        var query = from s in stations
                    where postcode.CompareTo(s.FromPostcode) > 0 && postcode.CompareTo(s.ToPostcode) < 0
                    select s;
        Console.WriteLine(query.ToList());
    }
}
public class DestinationStation
{
    public string FromPostcode;
    public string ToPostcode;

    public static List<DestinationStation> GetDestinationStations()
    {
        return new List<DestinationStation> {   new DestinationStation {FromPostcode = "A", ToPostcode = "C"},
                                                new DestinationStation {FromPostcode = "A", ToPostcode = "A"},
                                                new DestinationStation {FromPostcode = "C", ToPostcode = "C"},
                                                new DestinationStation {FromPostcode = "C", ToPostcode = "A"},
        };
    }
}
kmkemp
  • 1,656
  • 3
  • 18
  • 21
0

Assuming that your postcodes that you are using is an integer or similar ( not all postcodes are, e.g. the UK postcodes are like SW1A 1AA).

Console.WriteLine( stations.Any(station => postCode >= station.FromPostcode && station <= station.ToPostcode) );

Edit:

As a UK postcode defines four different levels of geographic unit you need to seperate the constituent parts so that you can compare them.

Phil Carson
  • 884
  • 8
  • 18
0

I have an List which each DestinationStation object has a FromPostcode and a ToPostcode, they are strings. I need to check if a given postcode is within any of the FromPostcodes and ToPostcodes for a given DestinationStation object...Make sense?

(my emphasis)

It sounds like you want to use the Any operator. It returns true is 'any' are found, otherwise false.

List<DestinationStation> stations = DestinationStation.GetDestinationStations(); 
var exists = stations.Any(s => 
    postcode <= s.FromPostcode && postcode >= s.ToPostcode);

if (exists)
    Console.WriteLine("It's within a range");

If you want to find within which range(s) your postcode was found, do a where / single / first.

var all = stations.Where(s => 
    postcode <= s.FromPostcode && postcode >= s.ToPostcode);

var first = stations.First(s => 
    postcode <= s.FromPostcode && postcode >= s.ToPostcode);

var only = stations.Single(s => 
    postcode <= s.FromPostcode && postcode >= s.ToPostcode);
Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169