Reputation:
How compare elements in different arrays or make how make block if ()
?
if string[] data = { "a", "b" };
that's array which have the info.
the string[] b = { "a" };
- checking array.
I must to check have array b
the element which have the data
array.
if in b
all element tha same like in data
i do something....
if string[] c = { "a", "b", "c", "d" };
- don't the same, because data
don't have "c" and "d"
I try to use Intersect().Any()
in block if
it doesn't work in case array c
because Any
find first rigth element
Upvotes: 0
Views: 122
Reputation: 38179
If data
is used only to do lookups, you can use a HashSet
instead:
HashSet<string> data = new HashSet<string>() { "a", "b" };
if (b.All(data.Contains))
{
// All the strings in b are in data
}
Otherwise you can use Except
as suggested by @mjwills:
if (!b.Except(data).Any())
{
// All the strings in b are in data
}
Upvotes: 0
Reputation: 460288
You can use !Except
+ Any
:
bool containsAll = !data.Except(yourArray).Any();
or probably less efficient with large arrays but maybe little bit more readable:
bool containsAll = data.All(yourArray.Contains);
Upvotes: 1