English 中文(简体)
LINQ询问是否在一系列的起步和结束式员额编码范围内检查员额编码?
原标题:LINQ query to check if a postcode is within a range of starting and ending postcodes?

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());
最佳回答

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;
问题回答

我现认为,自然的扼杀是你“之间”的含义。 如果情况并非如此,你应研究可比较的接口,以更多地控制订单。

我也把比较完全排除在外。 不过,你可以改变操作者,使之具有包容性。

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

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.

我有一份清单,每个目的地物体都有从Post代码和ToPost代码,它们是插图。 我需要检查一下,某一个定点代码是否在从Postcodes and ToPostcodes的 任何之内,以备某一目的地的物体......。

(我的强调)

希望使用<代码>。 任何操作员。 参看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");

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签