1

我需要检查 List 是否包含指定的 3 之外的任何值。

示例:我有这个 var sourceList = new List<string>("A", "B", "C", "D", "E");,我想检查该列表是否包含除“E”、“F”、“G”之外的任何值。只需一个 Bool 即可,sourceList它是用户将选择的预定义列表的动态子集。我的 BESIDES 列表将是静态的。

谢谢,

4

7 回答 7

6
var flag = sourceList.Except(new[] { "E", "F", "G" }).Any();
于 2013-07-10T13:01:57.107 回答
5

使用 LINQ:

bool contains = sourceList.Any(t => (t != "E" && t != "F" && t != "G"));
于 2013-07-10T12:59:51.693 回答
3

用户ExceptAny扩展来实现您的目标:

var sourceList = new List<string>{"A", "B", "C", "D", "E"};
var c = sourceList.Except( new List<string>{ "E", "F", "G"}).Any();
于 2013-07-10T13:02:37.310 回答
2
var sourceList = new List<string>("A", "B", "C", "D", "E");
var checkList = new List<string>("E", "F", "G");
bool anyBesides = sourceList.Except(checkList).Any();
于 2013-07-10T13:02:16.557 回答
1

不太希望创建一个新列表,Except并且当前的Any解决方案不能用于不同的字母组合。

Contains和的组合Any就足够了。

var sourceList = new List<string> { "A", "B", "C", "D", "E" };
var except = new[] { "E", "F", "G" };

var containsExcept = sourceList.Any(x => except.Contains(x));
于 2013-07-10T13:13:43.040 回答
0
var sourceList = new List<string>{"A", "B", "C", "D", "E"};

 bool res =sourceList.SequenceEqual(new string[] { "A", "B", "C"});

也不要忘记没有你写过的构造函数:

var sourceList = new List<string>("A", "B", "C", "D", "E");

它应该是:

var sourceList = new List<string>{"A", "B", "C", "D", "E"};
于 2013-07-10T13:07:30.747 回答
0
var sourceList = new List<string>{"A", "B", "C", "D", "E"};
var besidesList = new List<string>{"E", "F", "G"};  
var anyBesides = sourceList.Any(t => !besidesList.Contains(t));
于 2013-07-10T13:14:20.763 回答