user928770
user928770

Reputation: 191

how i can check multiple keys in List< keyvaluepair <string,string>>?

i have a list of keyvaluepair in c# and i want to check two keys means return true if both exist in all other it's return false.

can someone tell em how i can do this through writing one statement only like my code not worked in c#

(info.Exists(x => x.Key == "user" && x.Key == "pass"))

Upvotes: 0

Views: 1787

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1501103

It sounds like you want:

if (info.Any(x => x.Key == "user") && info.Any(x => x.Key == "pass"))

(I've used Any here so that it's more general to any IEnumerable<T> using LINQ, but you could use Exists for List<T> just as easily.)

Just as a fun alternative:

string[] requiredKeys = { "user, "pass" };
if (!requiredKeys.Except(info.Select(x => x.Key)).Any())
{
    ...
}

Upvotes: 3

Related Questions