Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare value to array of strings using StartsWith

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.

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

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

like image 756
pierre Avatar asked Nov 23 '11 10:11

pierre


People also ask

How to check string StartsWith c#?

C# | StartsWith() Method. In C#, StartsWith() is a string method. This method is used to check whether the beginning of the current string instance matches with a specified string or not. If it matches then it returns the string otherwise false.

How do you check for multiple StartsWith in Python?

To check if a given string starts with any of multiple prefixes , you can use the any(iterable) function that returns True if at least one of the values in the iterable evaluates to True . You can check each prefix against the string by using a generator expression like so: any(s. startswith(x) for x in prefixes) .


1 Answers

var result =  myCollection.Where(c =>  
                           exceptions.All(e => 
                                       !c.Property[3].Value.StartsWith(e));
like image 75
sll Avatar answered Nov 12 '22 22:11

sll