我需要检查 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();
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));