我希望我已经得到了你想要的。
void Main()
{
var numbers =new [] {1, 2, 3, 5};
//ordering to make sure that ranges is ascending order
var orderedRanges = new []{1, 1.5, 2.6, 4, 50}.OrderBy(v => v);
var valuesInRanges = numbers.Select(number =>
FormatValueInRange(
number,
orderedRanges.Last(range => range <= number ),
orderedRanges.First(range => range > number )));
var result = "Find " + string.Join("and ", valuesInRanges);
Console.WriteLine (result);
}
private string FormatValueInRange(double val, double lower, double upper)
{
return string.Format(@"{0} is {{{1}}} to {{{2}}} ", val, lower, upper);
}
印刷:
Find 1 is {1} to {1.5} and 2 is {1.5} to {2.6} and 3 is {2.6} to {4} and 5 is {4} to {50}