I'm trying to see if my source collection contains values which can only be from a 2nd collection.
For example.
Valid data collection: 1, 2, 5
Source Collection | Result
-----------------------------------------
<empty> | true
1 | true
1, 2 | true
1, 2, 5 | true
1, 5 | true
3 | false (3 is illegal)
1, 3 | false (3 is illegal)
1, 2, 555 | false (555 is illegal)
So it's like .. if my source collection has some values .. then the values can only exist if they are contained in the other collection.
Urgh. hard to explain :(
Something like
var allInCollection = src.All(x => valid.Contains(x));
Or if you prefer a loop-based approach:
bool result = true;
foreach(var e in src)
{
if (!valid.Contains(e)) result = false;
}
You could use LINQ
Except
To check if any of the element in the collection is not in the other collection.
For example:
var a = new List<int>() {1,2,5};
var b = new List<int>() {1,3};
var c = b.Except(a);
if (c.Any()){ //then it is wrong, some items of b is not in a
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With