我的RavenDB数据库中有两个不同的文档集合 - 团队和比赛。文档如下所示:
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问题。但是,这样一个按语句嵌套的组会是什么样子呢?或者可以用另一种方式完成?
您最好为此使用转换结果或包含。请参阅此处的文档:http://ravendb.net/docs/client-api/querying/handling-document-relationships