'Contains half a tuple
How can I check if a list contains a tuple partial match?
var tuples = new List<(int, int)>( new [] { (1, 1), (1, 2), (3, 4), (4, 2) } );
tuples.Contains((3, _));
Solution 1:[1]
You can use the pattern matching is operator, which is very close to the syntax you had in mind:
tuples.Any(x => x is (3, _));
Solution 2:[2]
You could write a custom Contains function, but a .Where will work just fine:
using System.Linq;
[...]
tuples.Where(tup => tup.Item1 == 3);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sweeper |
| Solution 2 | KYL3R |
