3

我有一个要求,我需要获取按比赛日期排序的比赛列表。但我还需要将我朋友举办的比赛添加到顶部。因此,我正在尝试维护一个索引,以记录我参加过每场比赛的朋友的数量。

我有以下对象:

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 中的计数以仅反映该跑步者朋友列表中存在的参加该比赛的人。

4

1 回答 1

1

我最终选择了一条不同的道路。Oren 表示您无法过滤源数据,这是有道理的。现在,我只需索引谁参加了什么比赛并在查询中应用 where in 过滤器,然后按日期顺序对所有比赛进行排序,并从上一个查询中删除这些比赛。

于 2012-09-16T05:03:22.493 回答