Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created May 9, 2012 22:56
Show Gist options
  • Save benfoster/2649514 to your computer and use it in GitHub Desktop.
Save benfoster/2649514 to your computer and use it in GitHub Desktop.
Raven multi map index
public class Vacancies_SummaryWithApplicationCount : AbstractMultiMapIndexCreationTask<VacancySummary>
{
public Vacancies_SummaryWithApplicationCount()
{
AddMap<Vacancy>(vacancies => from v in vacancies
select new
{
Id = v.Id,
Position = v.Position,
StateId = v.StateId,
DateSubmitted = v.DateSubmitted,
ApplicationCount = 0
});
AddMap<VacancyApplication>(applications => from a in applications
select new
{
Id = a.VacancyId,
Position = (string)null,
StateId = (string)null,
DateSubmitted = DateTime.MinValue,
ApplicationCount = 1,
});
Reduce = results => from result in results
group result by result.Id into summary
select new
{
Id = summary.Key,
Position = summary.Select(x => x.Position).Where(x => x != null).FirstOrDefault(),
StateId = summary.Select(x => x.StateId).Where(x => x != null).FirstOrDefault(),
DateSubmitted = summary.Select(x => x.DateSubmitted).Where(x => x != DateTime.MinValue).FirstOrDefault(),
ApplicationCount = summary.Sum(x => x.ApplicationCount)
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment