好的人,在抨击之前,这与我调查过的有点不同。
我目前有以下代码:
public async Task<ParticipantTournament> GetParticipantTournamentByDescending(int tournamentId, int participantId)
{
var response = await _dbRepositories.TournamentParticipantMatchRepository
.Where(x => x.TournamentId == tournamentId)
.OrderByDescending(y => y.TournamentMatch.RoundNumber)
.ThenByDescending(y => y.TournamentMatch.Id)
.Include(x => x.Tournament)
.Include(x => x.Participant1)
.Include(x => x.Participant2)
.Include(x => x.TournamentMatch)
.Select(z => new TournamentParticipantMatchLogicDto
{
IsLastMatch = OneTrue() // <==== Problem here,
TournamentParticipantMatch = z
}).Where(x => (x.TournamentParticipantMatch.Participant1Id == participantId || x.TournamentParticipantMatch.Participant2Id == participantId))
.ToListAsync();
return new ParticipantTournament
{
ParticipantMatches = response,
};
}
/**
* This may be something super dumb. But I couldnt' come up with something better.
* How do I detect the latest match?
* http://stackoverflow.com/questions/14687136/check-if-record-is-last-or-first-in-list-with-linq
*
* Well, after digging a little bit, I've found that this is not possible :o
* */
private bool OneTrue()
{
if (!IsLast) return false;
IsLast = false;
return true;
}
我正在建立一个锦标赛平台。我需要知道哪个是最后一个匹配,所以我可以给玩家5轮而不是3.而不是创建一个新的列并用虚假或真实填充它,我决定过滤它。我认为我可以利用LINQ的延迟执行:
可能的解决方案我认为它可行: - 创建一个布尔列,该列将保留最后一个匹配的“true”值。 - 使用规范模式(我不知道如何在这种情况下应用。尝试使用表达式和Func但无法正确映射它们) - 加载所有ID并选择那些ID中的最后一个。将这些ID与所有用户拥有的ID进行比较。不幸的是,这会为数据库添加额外的往返。
我该怎么办?
谢谢!
P.S:OneTrue()方法它执行的操作,它返回true一次,然后返回false。 (快速谷歌搜索后没有找到任何其他内容)
表格显示了我目前拥有的数据的模拟。我只需要提取当前用户需要的内容,因此我不需要其他2行(您可以在表#2中看到)。一旦我选择了这两行,我就排除了其他可能最后一次匹配的行,但是只选择这两行我就不知道了。我试图通过尝试从第一次尝试查询它来保存任何冗余。我知道最后一场比赛是比赛的最后一场
所以我试图做的是通过降序排序它们(因为它们是有序的),并选择最后一个作为最后一个匹配。
答案 0 :(得分:1)
您是否可以在使用linq返回数据后查询数据子集,例如:
var temp = from e in _dbRepositories.TournamentParticipantMatchRepository
where (from f in _dbRepositories.TournamentParticipantMatchRepository
where f.TournamentId == tournamentId)
.Include(x => x.Tournament)
.Include(x => x.Participant1)
.Include(x => x.Participant2)
.Include(x => x.TournamentMatch)
.Select(z => new TournamentParticipantMatchLogicDto
{
IsLastMatch = false, // <==== Problem here,
TournamentParticipantMatch = z
}).Where(x => (x.TournamentParticipantMatch.Participant1Id == participantId || x.TournamentParticipantMatch.Participant2Id == participantId))
.ToListAsync();
int maxResult= temp.Max(t => t.TournamentParticipantMatch.Id);
var update= temp.SingleOrDefault(x => x.TournamentParticipantMatch.Id== maxResult);
if(update!= null)
update.IsLastMatch= true;
答案 1 :(得分:0)
这就是我最终要做的事情。如果您看到任何改进,请告诉我!
public async Task<ParticipantTournament> GetParticipantTournamentByDescending(int tournamentId, int participantId)
{
var lastMatchId = await _dbRepositories.TournamentParticipantMatchRepository
.Where(x => x.TournamentId == tournamentId)
.OrderByDescending(y => y.TournamentMatch.RoundNumber)
.ThenByDescending(y => y.TournamentMatch.Id)
.Select(x => x.Id).FirstOrDefaultAsync();
var response = await _dbRepositories.TournamentParticipantMatchRepository
.Where(x => x.TournamentId == tournamentId)
.Where(x => (x.Participant1Id == participantId || x.Participant2Id == participantId))
.Include(x => x.Tournament)
.Include(x => x.Participant1)
.Include(x => x.Participant2)
.Include(x => x.TournamentMatch)
.ToListAsync();
var logic = response.Select(z=> new TournamentParticipantMatchLogicDto
{
IsLastMatch = z.Id == lastMatchId,
TournamentParticipantMatch = z
})
;
return new ParticipantTournament
{
ParticipantMatches = logic,
};
}