我的 RavenDB 数据库中有两个不同的文档集合 - Teams 和 Matches。文件如下所示:
public class Team {
public string Id { get; set; }
public string Name { get; set; }
public int LeaguePosition { get; set; }
}
public class Match {
public string Id { get; set; }
public string HomeTeamName { get; set; }
public string AwayTeamName { get; set; }
public DateTime StartTime { get; set; }
}
所以基本上我有球队和这些球队之间的比赛。但是,对于某些操作,我需要从数据库中获取一个类似于以下内容的实体:
public class MatchWithExtraData {
public string Id { get; set; } // Id from the match document.
public string HomeTeamId { get; set; }
public string HomeTeamName { get; set; }
public int HomeTeamPosition { get; set; }
public string AwayTeamId { get; set; }
public string AwayTeamName { get; set; }
public int AwayTeamPosition { get; set; }
public DateTime? StartTime { get; set; }
}
我想要的是真正的比赛文件,但有主客场球队的 id 和联赛位置的额外字段。基本上用两份球队文件加入主客队名称的比赛文件,一份用于主队,一份用于客队。我认为多映射/减少索引应该可以解决问题,所以我从以下索引开始:
public class MatchWithExtraDataIndex: AbstractMultiMapIndexCreationTask<MatchWithExtraData> {
public MatchWithExtraData() {
AddMap<Team>(
teams => from team in teams
select new {
Id = (string)null,
HomeTeamId = team.Id,
HomeTeamName = team.Name,
HomeTeamPosition = team.LeaguePosition,
AwayTeamId = team.Id,
AwayTeamName = team.Name,
AwayTeamPosition = team.LeaguePosition,
StartTime = (DateTime?)null
}
);
AddMap<Match>(
matches => from match in matches
select new {
Id = match.Id,
HomeTeamId = (string)null,
HomeTeamName = match.HomeTeamName,
HomeTeamPosition = 0,
AwayTeamId = (string)null,
AwayTeamName = match.AwayTeamName,
AwayTeamPosition = 0,
StartTime = match.StartTime
}
);
Reduce = results => from result in results
// NOW WHAT?
}
}
减少部分是我无法弄清楚的部分,因为每场比赛有两支球队。我想我需要先在 HomeTeamName 上,然后在 AwayTeamName 上做一个嵌套组,但我不知道该怎么做。
也许这更像是一个 LINQ 问题而不是 RavenDB 问题。但是这样一个嵌套的 group by 语句会是什么样子呢?或者可以通过其他方式完成吗?