Skip to content

Instantly share code, notes, and snippets.

@jasonporritt
Created August 26, 2011 11:11
Show Gist options
  • Save jasonporritt/1173204 to your computer and use it in GitHub Desktop.
Save jasonporritt/1173204 to your computer and use it in GitHub Desktop.
First or 404
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