找出我已经认识的玩家
Find out which players I've already met
我有以下数据结构:
public class Match
{
public List<Player> Participants;
}
public class Player
{
public string Name;
}
现在我得到了以下示例数据:
Match 1
- PlayerA
- PlayerB
- PlayerC
Match 2
- PlayerA
- PlayerB
- PlayerD
- PlayerE
根据此数据,如果 PlayerA
想知道他与哪些球员一起比赛,答案将是 PlayerB, PlayerC, PlayerD and PlayerE
。
我现在的问题是,如果我想显示哪个玩家和谁一起玩和,那么最合乎逻辑且最容易查询的数据结构是什么?多久一次。毕竟我想向用户展示这样的图表(超级绘画技巧):
你可以这样做,使用 Linq
List<List<Match> matches = ...
string player = "playerA";
var coplayers = matches.Where(x=>x.Any(p=>p.Participants.Any(s=>s.Name == player))) // get all participants where group contains participant.
.SelectMany(x=> x.SelectMany(p=>p.Participants)) // get all participants where group contains participant.
.Where(x=>x.Name != player) // List co participants
.GroupBy(x=>x.Name) // Distinct by grouping on Name or( need to override equal)
.Select(x=>x.FirstOrDefault())
.ToList()
勾选这个Demo
作为使您的解决方案更简洁的建议,让 Player 覆盖 Equals。代码如下所示:
public class Player
{
public override bool Equals(object obj)
{
var other = obj as Player;
if (other == null)
return false;
return this.Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
public string Name;
}
这使您可以按照
的思路制定更具可读性的寻找对手的解决方案
Player me = new Player { Name = "Me" };
var allMatches = new List<Match>
{
new Match
{
Participants = new List<Player>
{
me,
new Player { Name = "Some Other Dude"}
}
},
new Match
{
Participants = new List<Player>
{
me,
new Player { Name = "My Rival" }
}
}
};
var myMatches = allMatches.Where(m => m.Participants.Contains(me)).ToList();
var myOpponents = myMatches.SelectMany(m => m.Participants.Except(new [] {me})).Distinct();
我有以下数据结构:
public class Match
{
public List<Player> Participants;
}
public class Player
{
public string Name;
}
现在我得到了以下示例数据:
Match 1
- PlayerA
- PlayerB
- PlayerC
Match 2
- PlayerA
- PlayerB
- PlayerD
- PlayerE
根据此数据,如果 PlayerA
想知道他与哪些球员一起比赛,答案将是 PlayerB, PlayerC, PlayerD and PlayerE
。
我现在的问题是,如果我想显示哪个玩家和谁一起玩和,那么最合乎逻辑且最容易查询的数据结构是什么?多久一次。毕竟我想向用户展示这样的图表(超级绘画技巧):
你可以这样做,使用 Linq
List<List<Match> matches = ...
string player = "playerA";
var coplayers = matches.Where(x=>x.Any(p=>p.Participants.Any(s=>s.Name == player))) // get all participants where group contains participant.
.SelectMany(x=> x.SelectMany(p=>p.Participants)) // get all participants where group contains participant.
.Where(x=>x.Name != player) // List co participants
.GroupBy(x=>x.Name) // Distinct by grouping on Name or( need to override equal)
.Select(x=>x.FirstOrDefault())
.ToList()
勾选这个Demo
作为使您的解决方案更简洁的建议,让 Player 覆盖 Equals。代码如下所示:
public class Player
{
public override bool Equals(object obj)
{
var other = obj as Player;
if (other == null)
return false;
return this.Name == other.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
public string Name;
}
这使您可以按照
的思路制定更具可读性的寻找对手的解决方案Player me = new Player { Name = "Me" };
var allMatches = new List<Match>
{
new Match
{
Participants = new List<Player>
{
me,
new Player { Name = "Some Other Dude"}
}
},
new Match
{
Participants = new List<Player>
{
me,
new Player { Name = "My Rival" }
}
}
};
var myMatches = allMatches.Where(m => m.Participants.Contains(me)).ToList();
var myOpponents = myMatches.SelectMany(m => m.Participants.Except(new [] {me})).Distinct();