You can group kList and then use SequenceEqual with tid , here is a demo
List<KeyValuePair<string, int>> kList = new List<KeyValuePair<string, int>>() { new KeyValuePair<string, int>("001",1), new KeyValuePair<string, int>("001",2), new KeyValuePair<string, int>("002",1), new KeyValuePair<string, int>("003",3), new KeyValuePair<string, int>("004",1), new KeyValuePair<string, int>("004",2) }; List<int> tid = new List<int>() { 1, 2 }; var query = kList.GroupBy(i => i.Key) .Where(g => g.Select(j => j.Value).Distinct().OrderBy(i => i).SequenceEqual(tid)) .SelectMany(g => g.Select(x => x.Key)).Distinct(); Console.WriteLine(string.Join(",", query));
and maybe SequenceEqual has some problem, if you want contains all tid , not Equal , then you can use Intersect for instand SequenceEqual , the code is as follows
List<KeyValuePair<string, int>> kList = new List<KeyValuePair<string, int>>() { new KeyValuePair<string, int>("001",1), new KeyValuePair<string, int>("001",2), new KeyValuePair<string, int>("002",1), new KeyValuePair<string, int>("001",3),