LINQ 查询检查邮政编码是否在开始和结束邮政编码的范围内?
我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用
CompareTo
来查找字符串。这有效吗?Try
CompareTo
for strings. Does this work?我假设字符串的自然顺序就是您所说的“之间”的意思。如果情况并非如此,您应该查看 IComparable 接口以更好地控制排序。
我还进行了独家比较。不过,您可以更改运算符以使其具有包容性。
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.
假设您使用的邮政编码是整数或类似的(并非所有邮政编码都是整数,例如英国邮政编码类似于 SW1A 1AA)。
编辑:
由于英国邮政编码定义了四个不同级别的地理单位,因此您需要将各个组成部分分开,以便可以对它们进行比较。
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).
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.
(我的重点)
听起来您想使用
Any
运算符。如果找到“任何”,则返回true
,否则返回false
。如果您想查找您的邮政编码是在哪个范围内找到的,请先执行“where / single /”。
(my emphasis)
It sounds like you want to use the
Any
operator. It returnstrue
is 'any' are found, otherwisefalse
.If you want to find within which range(s) your postcode was found, do a where / single / first.