Skip to content

FirstOrFallback and LastOrFallback #67

@atifaziz

Description

@atifaziz

Just ran into a situation where FirstOrFallback would have been nice to have. I have a list of ranges specified by an editor of the website. The user enters a number on the site and I need to find the correct range. I have no control over the editor and the way he defines the ranges so there is a possiblity that ranges overlap or that the number entered by the user falls outside the ranges. So something like this would do it:

var rangeWithLargestCeiling = ranges.MaxBy(x=>x.Ceiling).Single();

var range = ranges
    .Where(x => IsInRange(number,x))
    .FirstOrFallback(() => rangeWithLargestCeiling);

This will handle both the overlapping issue (where there is more than one range where the number is in range) and the fallback.


Originally reported on Google Code with ID 67

Reported by hammerskov on 2011-06-07 07:05:59

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions