Skip to content

Instantly share code, notes, and snippets.

@amiry-jd
Last active December 15, 2015 01:19
Show Gist options
  • Save amiry-jd/5178900 to your computer and use it in GitHub Desktop.
Save amiry-jd/5178900 to your computer and use it in GitHub Desktop.
// documents: Place
public class Place{
public int Id{get;set;}
public string Name{get;set;}
public IList<int> PhotoIds{get;set;}
}
// documents: Photo
public class Photo{
public int Id{get;set;}
public string Title{get;set;}
}
// index definition
public class PlaceWithCoverPhotoIndex : AbstractIndexCreationTask<Place> {
public PlaceWithCoverPhotoIndex() {
Map = places =>
from place in places
let firstPhotoId = place.PhotoIds.FirstOrDefault()
select new { CoverPhotoId = firstPhotoId };
TransformResults =
(database, places) =>
from place in places
let firstPhotoId = "photos/" + place.PhotoIds.FirstOrDefault()
let cover = database.Load<Photo>(firstPhotoId)
select new {
Id = place.Id,
Name = place.Name,
CoverPhoto = new {
PhotoId = cover.Id,
Title = cover.Title,
}
};
}
}
// usage
public void Client(){
using(var session = store.OpenSession()){
var list = session.Query<Place,PlaceWithCoverPhotoIndex>().ToList(); // returns empty list
}
}
@Morganedo
Copy link

Hi, please my name is Morgan, can you please teach me how i can create stuff like this on my own? please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment