LINQ 查询检查邮政编码是否在开始和结束邮政编码的范围内?

发布于 2024-12-13 18:55:41 字数 372 浏览 0 评论 0原文

我需要使用 linq 检查邮政编码是否在给定的开始和结束邮政编码内。

这是我到目前为止所拥有的,但根本不正确,有人可以指出我正确的方向吗?

List<DestinationStation> stations = DestinationStation.GetDestinationStations();
var query = from s in stations
            where postcode <= s.FromPostcode && postcode >= s.ToPostcode
            select s;
Console.WriteLine(query.ToList());

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());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难得心□动 2024-12-20 18:55:41

尝试使用 CompareTo 来查找字符串。这有效吗?

var query =
    from s in stations
    where postcode.CompareTo(s.FromPostcode) >= 0
    where postcode.CompareTo(s.ToPostcode) <= 1
    select s;

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;
岁月染过的梦 2024-12-20 18:55:41

我假设字符串的自然顺序就是您所说的“之间”的意思。如果情况并非如此,您应该查看 IComparable 接口以更好地控制排序。

我还进行了独家比较。不过,您可以更改运算符以使其具有包容性。

    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"},
        };
    }
}

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"},
        };
    }
}
短叹 2024-12-20 18:55:41

假设您使用的邮政编码是整数或类似的(并非所有邮政编码都是整数,例如英国邮政编码类似于 SW1A 1AA)。

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

编辑:

由于英国邮政编码定义了四个不同级别的地理单位,因此您需要将各个组成部分分开,以便可以对它们进行比较。

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.

苍暮颜 2024-12-20 18:55:41

我有一个列表,其中每个 DestinationStation 对象都有一个 FromPostcode 和一个 ToPostcode,它们是字符串。我需要检查给定的邮政编码是否在给定 DestinationStation 对象的 FromPostcodes 和 ToPostcodes 的任何范围内...有意义吗?

(我的重点)

听起来您想使用 Any 运算符。如果找到“任何”,则返回 true,否则返回 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");

如果您想查找您的邮政编码是在哪个范围内找到的,请先执行“where / single /”。

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文