Reputation: 1
I need piece of code to resolve below issue.
Tuple is defined like (12, 2) (12,3) (15, 7) (15,7) (15,7) .
I need to extract 2,3,7,7 and 7 (item2) in a List based on item1(Which acts like key). For Example 12 and 15 are the Keys(item1).
Based on the keys it should return the list of values like if 12 given then it should return List of values like 2 and 3 . If 15 given then it should return list of values like 7,7 and 7.
Upvotes: 0
Views: 1266
Reputation: 81493
If you were to do this often, it would make sense using a dictionary and access your values via an indexer
var dict = list
.GroupBy(x => x.Item1)
.ToDictionary(x => x.Key, x => x.Select(y => y.Item2).ToList());
// how to access your values via an indexer
var yourValues = dict[12];
Upvotes: 1
Reputation: 5523
You can do that like this.
var result = pairs.Where(p => p.Item1 == itemToSearch).Select(p => p.Item2).ToList();
Upvotes: 3