Skip to content

Instantly share code, notes, and snippets.

@kristofclaes
Created June 5, 2011 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kristofclaes/1008780 to your computer and use it in GitHub Desktop.
Save kristofclaes/1008780 to your computer and use it in GitHub Desktop.
Retrieving next and previous records with Simple.Data
// This code works, but somehow it looks like there is a better and more elegant solution for it.
IEnumerable<Models.Photo> previous = DB.Photos
.FindAll(DB.Photos.Published == true && DB.Photos.DatePublished < currentPhoto.DatePublished.Value)
.OrderByDatePublishedDescending()
.Take(1)
.Cast<Models.Photo>();
int? previousPhotoId = null;
Models.Photo previousPhoto = previous.FirstOrDefault();
if (previousPhoto != null)
{
previousPhotoId = previousPhoto.Id;
}
IEnumerable<Models.Photo> next = DB.Photos
.FindAll(DB.Photos.Published == true && DB.Photos.DatePublished > currentPhoto.DatePublished.Value)
.OrderByDatePublished()
.Take(1)
.Cast<Models.Photo>();
int? nextPhotoId = null;
Models.Photo nextPhoto = next.FirstOrDefault();
if (nextPhoto != null)
{
nextPhotoId = nextPhoto.Id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment