我正在尝试从存储在字典中的列表中选择特定元素。
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();
理想情况下,它应该返回与 killer 姓名匹配的所有 Teamkills 的列表。
请您参考如下方法:
如果我了解您想要的结果,您可能只需使用 Values , SelectMany 和 Where .
var result = teamkillCounter.SelectMany(x => x.Value.Where(y => y.killerName.Contains("bobo")));
Dictionary<TKey,TValue>.Values Property
Gets a collection containing the values in the Dictionary.
Projects each element of a sequence to an
IEnumerable<T>and flattens the resulting sequences into one sequence.
