我有一个Dictionary<string, User>
.
User
是一个具有以下属性的对象UID
, UNIQUE KEY
和更多。我的字典键是UNIQUE KEY
用户的。
现在,我想得到一个User
从我的字典值中UID
而不是钥匙,比如ContainsKey
..如何使用 lambda expr 或 linq 完成它?有没有好的解决方案?
这是一个工作示例:
using System;
using System.Collections.Generic;
using System.Linq;
internal class User
{
public string ID { get; set; }
public string Name { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
Dictionary<string, User> dic = new Dictionary<string, User>();
dic.Add("1", new User { ID = "id1", Name = "name1" });
dic.Add("2", new User { ID = "id2", Name = "name2" });
dic.Add("3", new User { ID = "id3", Name = "name3" });
User user = dic.Where(z => z.Value.ID == "id2").FirstOrDefault().Value;
Console.ReadKey();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)