user3310334
user3310334

Reputation:

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, _));

Upvotes: 2

Views: 166

Answers (3)

Sweeper
Sweeper

Reputation: 270890

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, _));

Upvotes: 2

KYL3R
KYL3R

Reputation: 4073

You could write a custom Contains function, but a .Where will work just fine:

using System.Linq;
[...]

tuples.Where(tup => tup.Item1 == 3);

Upvotes: 0

PMF
PMF

Reputation: 17185

Using Linq:

var tuples = new List<(int, int)>(new[] { (1, 1), (1, 2), (3, 4), (4, 2) });
if (tuples.Any(x => x.Item1 == 3))
{
   //....
}

Upvotes: 2

Related Questions