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.
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.
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) .
var result = myCollection.Where(c =>
exceptions.All(e =>
!c.Property[3].Value.StartsWith(e));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With