我正在尝试查看我的源集合是否包含只能来自第二个集合的值。

例如。

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)

所以就像 .. 如果我的源集合有一些值 .. 那么这些值只有在它们包含在另一个集合中时才能存在。

呃。难以解释 :(

最佳答案

就像是

var allInCollection = src.All(x => valid.Contains(x));

或者,如果您更喜欢基于循环的方法:
bool result = true;
foreach(var e in src)
{
    if (!valid.Contains(e)) result = false;
}

关于c# - 如何检查一个集合是否仅包含来自另一个集合的元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36590747/

10-12 01:44
查看更多