Last active
December 15, 2015 01:19
-
-
Save amiry-jd/5178900 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, please my name is Morgan, can you please teach me how i can create stuff like this on my own? please