c# – 使用Linq创建一个字典
发布时间:2020-12-16 00:06:03 所属栏目:百科 来源:网络整理
导读:如何使用 Linq创建一个Dictionary(甚至更好,一个ConcurrentDictionary)? 例如,如果我有以下XML students student name="fred" address="home" avg="70" / student name="wilma" address="home,HM" avg="88" / . . (more student blocks) ./students 加载到X
如何使用
Linq创建一个Dictionary(甚至更好,一个ConcurrentDictionary)?
例如,如果我有以下XML <students> <student name="fred" address="home" avg="70" /> <student name="wilma" address="home,HM" avg="88" /> . . (more <student> blocks) . </students> 加载到XDocument doc;并希望填充ConcurrentDictionary< string,Info> (其中关键是名称和信息是一些持有地址和平均值的类.填充信息现在不是我的关注),我该怎么做? 解决方法XDocument xDoc = XDocument.Parse(xml); var dict = xDoc.Descendants("student") .ToDictionary(x => x.Attribute("name").Value,x => new Info{ Addr=x.Attribute("address").Value,Avg = x.Attribute("avg").Value }); var cDict = new ConcurrentDictionary<string,Info>(dict); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |