使用StartsWith将值与字符串数组进行比较

时间:2021-07-01 21:20:52

I have an array:

我有一个数组:

string[] exceptions = new string[] { "one", two", "one_1", "three" };

.. I want to be able to say:

..我希望能够说:

var result = from c in myCollection
             where not c.Property[3].Value.StartWith(exceptions)
             select c;

So I want myCollection to be filtered to only show those records whose Property[3].Value does not StartWith a value in the exceptions array. I know StartsWith doesn't take a collection so I'm unsure if this is possible via LINQ or not.

所以我希望过滤myCollection只显示那些Property [3] .Value不会在例外数组中启动一个值的记录。我知道StartsWith没有采集集合所以我不确定这是否可以通过LINQ实现。

Is this possible in LINQ?! Or am I trying to shoehorn my problem into a LINQ solution?

这在LINQ中是否可行?!或者我是否试图将我的问题转变为LINQ解决方案?

EDIT: I should say, Contains is not an option since I only want to exclude elements whose property startswith the exception string.

编辑:我应该说,包含不是一个选项,因为我只想排除其属性以异常字符串开头的元素。

5 个解决方案

#1


12  

var result =  myCollection.Where(c =>  
                           exceptions.All(e => 
                                       !c.Property[3].Value.StartsWith(e));

#2


2  

Try this:

尝试这个:

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;

#3


2  

You could use IndexOfAny (and check result is index position zero) as that takes a collection.

你可以使用IndexOfAny(并检查结果是索引位置为零),因为它采用了一个集合。

#4


0  

You can select the collection of item you don't want then do a IEnumerable.Except().

您可以选择不需要的项目集合,然后执行IEnumerable.Except()。

I should look like this :

我应该看起来像这样:

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)));

#5


0  

var result = myCollection
              .where(
                 rs=>exceptions
                 .where(
                     rs1=>!rs.property[3].value.startsWith(rs1)
                  )
              )

#1


12  

var result =  myCollection.Where(c =>  
                           exceptions.All(e => 
                                       !c.Property[3].Value.StartsWith(e));

#2


2  

Try this:

尝试这个:

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;

#3


2  

You could use IndexOfAny (and check result is index position zero) as that takes a collection.

你可以使用IndexOfAny(并检查结果是索引位置为零),因为它采用了一个集合。

#4


0  

You can select the collection of item you don't want then do a IEnumerable.Except().

您可以选择不需要的项目集合,然后执行IEnumerable.Except()。

I should look like this :

我应该看起来像这样:

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)));

#5


0  

var result = myCollection
              .where(
                 rs=>exceptions
                 .where(
                     rs1=>!rs.property[3].value.startsWith(rs1)
                  )
              )