Skip to content

Instantly share code, notes, and snippets.

@phinett
Created February 28, 2012 12:37
Show Gist options
  • Save phinett/1932299 to your computer and use it in GitHub Desktop.
Save phinett/1932299 to your computer and use it in GitHub Desktop.
MultiMap Index - RavenDb
public class GroupsIndex_ByAccount : AbstractMultiMapIndexCreationTask<GroupsIndex_ByAccount.ReduceResult>
{
public class ReduceResult
{
public string Id { get; set; }
public string GroupId { get; set; }
public string Name { get; set; }
public string AccountId { get; set; }
public DateTimeOffset DateJoined { get; set; }
public DateTimeOffset LastVisitedDate { get; set; }
public int MemberCount { get; set; }
}
public GroupsIndex_ByAccount()
{
AddMap<Group>(groups => from grp in groups
select new
{
grp.Id,
GroupId = grp.Id,
grp.Name,
AccountId = (string)null,
DateJoined = (object)null,
LastVisitedDate = (object)null,
MemberCount = 0
});
AddMap<GroupMember>(members => from member in members
select new
{
Id = member.GroupId,
GroupId = member.GroupId,
Name = (string)null,
AccountId = member.AccountId,
DateJoined = member.DateJoined,
LastVisitedDate = member.LastVisitedDate,
MemberCount = 0
});
AddMap<GroupMember>(members => from member in members
select new
{
Id = member.GroupId,
GroupId = member.GroupId,
Name = (string)null]=
AccountId = (string)null,
DateJoined = (object)null,
LastVisitedDate = (object)null,
MemberCount = 1
});
Reduce = results => from result in results
group result by result.Id
into g
select new
{
Id = g.Key,
GroupId = g.Select(x => x.GroupId).Where(x => x != null).FirstOrDefault(),
Name = g.Select(x => x.Name).Where(x => x != null).FirstOrDefault(),
AccountId = g.Select(x => x.AccountId).Where(x => x != null).FirstOrDefault(),
DateJoined = g.Select(x => x.DateJoined).Where(x => x != null).FirstOrDefault(),
LastVisitedDate = g.Select(x => x.LastVisitedDate).Where(x => x != null).FirstOrDefault(),
MemberCount = g.Sum(x => x.MemberCount)
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment