检查列表<;字符串>;包含除指定之外的任何值
本文关键字:任何值 包含 lt 列表 字符串 gt 检查 | 更新日期: 2023-09-27 18:21:44
我需要检查List是否包含指定的3之外的任何值。
示例:我有这个var sourceList = new List<string>("A", "B", "C", "D", "E");
,我想检查列表是否包含除"E"、"F"、"G"之外的任何值。只有Bool是可以的,sourceList
是用户可能选择的预定义列表的动态子集。我的BESIDES列表将是静态的。
谢谢,
var flag = sourceList.Except(new[] { "E", "F", "G" }).Any();
使用LINQ:
bool contains = sourceList.Any(t => (t != "E" && t != "F" && t != "G"));
使用Except
和Any
扩展来实现您的目标:
var sourceList = new List<string>{"A", "B", "C", "D", "E"};
var c = sourceList.Except( new List<string>{ "E", "F", "G"}).Any();
var sourceList = new List<string>("A", "B", "C", "D", "E");
var checkList = new List<string>("E", "F", "G");
bool anyBesides = sourceList.Except(checkList).Any();
不太希望使用Except
创建新列表,并且当前的Any
解决方案不能用于不同的字母组合。
CCD_ 7和CCD_。
var sourceList = new List<string> { "A", "B", "C", "D", "E" };
var except = new[] { "E", "F", "G" };
var containsExcept = sourceList.Any(x => except.Contains(x));
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"};
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));