Created
August 26, 2011 11:11
-
-
Save jasonporritt/1173204 to your computer and use it in GitHub Desktop.
First or 404
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
public static class EnumerableUtils { | |
// First item, or throw 404 exception | |
public static TSource FirstOr404<TSource>(this IEnumerable<TSource> source) | |
{ | |
try | |
{ | |
return source.First(); | |
} | |
catch (InvalidOperationException e) | |
{ | |
throw new HttpException(404, "Not Found"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment