Skip to content

Instantly share code, notes, and snippets.

@kristofclaes
Created June 9, 2011 17:43
Show Gist options
  • Save kristofclaes/1017271 to your computer and use it in GitHub Desktop.
Save kristofclaes/1017271 to your computer and use it in GitHub Desktop.
Getting the previous photo
// When I run this code when no previous photo should be found, I get
// an exception saying "Query returned multiple rows; cannot return
// scalar value.". This is strange because it shouldn't return _any_
// values and because I use ".Take(1)" to make sure only one row is
// returned.
string previousSlug = DB.Photos
.Query()
.Select(DB.Photos.Slug)
.Where(DB.Photos.Published == true && DB.Photos.DatePublished < photo.DatePublished.Value) // There is no published photo with a smaller DatePublished than the one I check against
.OrderByDatePublishedDescending()
.Take(1) // Should return only one record no matter what
.ToScalarOrDefault<string>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment