Dictionary

从字典(DicRoleForScope)中查询对应(role)的值再赋值给其他变量( DicSelectedRoleForScope)

Dictionary<string, object> dic = DicRoleForScope.Where(s => s.Value.ToString().Contains(role)).
Select(q => new { q.Key, q.Value }).
ToList().AsEnumerable().ToDictionary(d => d.Key, d => (object)d.Value); // where对比中使用Contains

KeyValuePair<string, object> kvp = dic.FirstOrDefault();

if (kvp.Key != null && kvp.Value != null) //注意判断为空否
{
DicSelectedRoleForScope.Add(kvp.Key, kvp.Value); //Dictionary只能以键值对的形式添加
}

原文地址:https://www.cnblogs.com/mamaxiaoling/p/11439095.html