我假设字符串的自然顺序是您所说的“之间”。如果不是这样,您应该查看 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"},
};
}
}