我正在尝试从列表中选择要存储在词典中的特定元素。
internal Dictionary<string, List<Teamkill>> teamkillCounter = new Dictionary<string, List<Teamkill>>();
class Teamkill
{
public string killerName;
public string killerSteamId;
public TeamRole killerTeamRole;
public string victimName;
public string victimSteamId;
public TeamRole victimTeamRole;
public DamageType damageType;
}
// quotedArgs[1] is the killer name being searched for
// First attempt
List<Teamkill> teamkills = this.plugin.teamkillCounter.Select(x => x.Value.Select(y => y.killerName.Contains(quotedArgs[1])).Cast<List<Teamkill>>()).Cast<List<Teamkill>>().Single();
// Second attempt
List<Teamkill> teamkills = new List<Teamkill>();
teamkills = this.plugin.teamkillCounter.Values.SelectMany(x => x).ToList();
teamkills = teamkills.Where(y => y.killerName.Contains(quotedArgs[1])).ToList();
理想情况下,它应该返回与杀手的名字匹配的所有Teamkill的列表。
答案 0 :(得分:4)
如果我了解您想要的结果,则可以只使用Values
,SelectMany
和Where
。
var result = teamkillCounter.SelectMany(x => x.Value.Where(y => y.killerName.Contains("bobo")));
Dictionary<TKey,TValue>.Values
Property
获取一个集合,其中包含 字典。
将序列的每个元素投影到
IEnumerable<T>
并展平 产生的序列变成一个序列。