Reputation: 191
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
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