How to select values and key in a Dictionary of lists?
我有这些数据:
1 2 3 4 5 6 7 8 | public class MetaLink { public long LinkNumbering { get; set; } public long TargetPageId { get; set; } public string TargetUrl { get; set; } public LinkType LinkOfType { get; set; } } public static ConcurrentDictionary<int, List<MetaLink>> Links = new ConcurrentDictionary<int, List<MetaLink>>(); |
如何获取列表dictionary值中
也许Linq是可行的,但我找不到。我这样做:
1 | var someLinks = Links.Values.Where(kvp => kvp.Any(ml => ml.TargetUrl =="Some value")); |
但我拿不到相应的字典int键
你很亲近-你想要
1 2 3 | var someLinks = Links.Where(kvp => kvp.Value.Any(ml => ml.TargetUrl =="Some value")) // all key.value pairs where the Value contains the target URL .Select(kvp => kvp.Key); //keys for those values |
试试这个。尚未编译。
1 | var key = Links.Where(kvp => kvp.Value.Any(ml => ml.TargetUrl =="Some value")).Select(x => x.Key).SingleOrDefault(); |