c# – 允许使用来自LINQ查询的ToDictionary()重复键
发布时间:2020-12-16 01:41:38 所属栏目:百科 来源:网络整理
导读:我需要字典中的Key / Value内容.我不需要的是它不允许重复的密钥. Regex template = new Regex(@"{(?key.+?)}(?value[^{}]*)");IDictionarystring,string dictionary = template.Matches(MyString) .CastMatch() .ToDictionary(x = x.Groups["key"].Value,
我需要字典中的Key / Value内容.我不需要的是它不允许重复的密钥.
Regex template = new Regex(@"{(?<key>.+?)}(?<value>[^{}]*)"); IDictionary<string,string> dictionary = template.Matches(MyString) .Cast<Match>() .ToDictionary(x => x.Groups["key"].Value,x => x.Groups["value"].Value); 如何返回允许重复键的词典? 解决方法
使用
Lookup类:
Regex template = new Regex(@"{(?<key>.+?)}(?<value>[^{}]*)"); ILookup<string,string> dictionary = template.Matches(MyString) .Cast<Match>() .ToLookup(x => x.Groups["key"].Value,x => x.Groups["value"].Value); 编辑:如果您希望获得“普通”结果集(例如{key1,value1},{key1,value2},{key2,value2}而不是{key1,{value1,value2}},{value2}} )你可以得到类型 Regex template = new Regex(@"{(?<key>.+?)}(?<value>[^{}]*)"); ILookup<string,string> dictionary = template.Matches(MyString) .Cast<Match>() .Select(x => new KeyValuePair<string,string>( x.Groups["key"].Value,x.Groups["value"].Value ) ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Oracle Undo回滚段深入解析_超越OCP精通Oracle视频课程培训
- c# – Observable.Interval等到动作完成
- 如何创建一个原生的模式视图segue与反应本机?
- ruby-on-rails-3 – 为简单表单添加复选框,不在模型中输入
- ArcGIS Viewer for Flex(Layer 层?标签)
- 如何在C中编写缓存友好的多态代码?
- MODEL-View-Presenter(MVP)模式在FLEX下的开发实例
- c# – 从基类调用事件
- objective-c – containerURLForSecurityApplicationGroupI
- swift – NSCalendarUnit标志设置所??有标志