使用StartsWith将值与字符串数组进行比较
我有一个数组:
string[] exceptions = new string[] { "one", two", "one_1", "three" };
..我希望能够说:
var result = from c in myCollection where not c.Property[3].Value.StartWith(exceptions) select c;
所以我希望过滤myCollection
只显示那些Property[3].Value
不会在例外数组中StartWith
一个值的记录。 我知道StartsWith没有采集集合所以我不确定这是否可以通过LINQ实现。
这在LINQ中是否可行?! 或者我是否试图将我的问题转变为LINQ解决方案?
编辑:我应该说,包含不是一个选项,因为我只想排除其属性以exception字符串开头的元素。
var result = myCollection.Where(c => exceptions.All(e => !c.Property[3].Value.StartsWith(e));
试试这个:
string[] exceptions = new string[] { "one", "two", "one_1", "three" }; var result = from c in myCollection where !exceptions.Any(exception => c.Property[3].Value.StartsWith(exception)) select c;
你可以使用IndexOfAny(并检查结果是索引位置为零),因为它采用了一个集合。
您可以选择不需要的项目集合,然后执行IEnumerable.Except()。
我应该看起来像这样:
var result = from c in myCollection where not c.Property[3].Value.StartWith(exceptions) select c; var finalResult = myCollection.Except(myCollection.Select(i => i.StartWith(exception)));
var result = myCollection .where( rs=>exceptions .where( rs1=>!rs.property[3].value.startsWith(rs1) ) )