我有一个要求,我需要获取按比赛日期排序的比赛列表。但我还需要将我朋友举办的比赛添加到顶部。因此,我正在尝试维护一个索引,以记录我参加过每场比赛的朋友的数量。
我有以下对象:
public class Race {
public string Id { get; set; }
public string Name { get; set; }
public string Distance { get; set; }
public DateTime DateOfEvent { get; set; }
public string Location { get; set; }
}
public class Runner {
public string Id { get; set; }
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
public string Hometown { get; set; }
public List<string> Friends { get; set; }
public List<string> RacesRun { get; set; }
}
到目前为止,这就是我所拥有的索引:
public class RacesByFriends : AbstractMultiMapIndexCreationTask<RacesByFriends.ReduceResult> {
public class ReduceResult {
public string RunnerId { get; set; }
public string RaceId { get; set; }
public int Count { get; set; }
}
public RacesByFriends() {
AddMap<Runner>(runners => from runner in runners
from race in runner.RacesRun
select new {
RunnerId = runner.Id,
RaceId = race,
Count = 1,
});
AddMap<Race>(races => from race in races
select new {
RunnerId = (string)null,
RaceId = race.Id,
Count = 0,
});
Reduce = results => from result in results
group result by new {
result.RunnerId,
result.RaceId
} into g
select new {
RunnerId = g.Select(x => x.RunnerId).FirstOrDefault(),
RaceId = g.Select(x => x.RaceId).FirstOrDefault(),
Count = g.Sum(x => x.Count)
};
}
}
我的问题是,我如何获得 ReduceResult 中的计数以仅反映该跑步者朋友列表中存在的参加该比赛的人。