0

I have this data :

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>>();

How can I obtain all index of MetaLink object in the list dictionnary values and the correspondig dictionnary key with TargetUrl property == "Some value"

Perhaps is possible with Linq, but I don't find it. I do this :

var someLinks = Links.Values.Where(kvp => kvp.Any(ml => ml.TargetUrl == "Some value"));

But I can't get the correspondig dictionnary int key

LeMoussel
  • 5,290
  • 12
  • 69
  • 122
  • Your run-on sentence is really hard to parse. It would be much easier to help if you if you'd give a concrete example with sample input and expected output - ideally with what you've tried so far, too. – Jon Skeet Oct 05 '15 at 15:54

2 Answers2

1

You're close - you want

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
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

Give a try on this. Haven't compiled.

var key = Links.Where(kvp => kvp.Value.Any(ml => ml.TargetUrl == "Some value")).Select(x => x.Key).SingleOrDefault();
HashCoder
  • 946
  • 1
  • 13
  • 32