I am trying to create an extension "WhereNot"
So I can use:
Dim x = "Hello world "
Dim y = x.Split.WhereNot(AddressOf String.IsNullOrEmpty)
Note that my aim here is to learn linq expressions; not solve my issue.
I craated this function:
<Extension()> _
Public Function WhereNot(Of TElement)(ByVal source As IQueryable(Of TElement), ByVal selector As Expression(Of Func(Of TElement, Boolean))) As IQueryable(Of TElement)
Return source.Where(GetWhereNotExpression(selector))
End Function
I don't know how to switch the boolean flag, will the function Negate do it?
answers in both vb.net and C# are welcommed
Yes a Negate method like this will help you:
And then use it like this:
Or with a WhereNot() method like this:
I realize this is a very old answered question, but I think the selected answer is misleading, because the asker was looking for an expression, and the selected answer provides a lambda.
This means that its invocation from an IQueryable will return an
IEnumerable<T>
, not anIQueryable<T>
. This forces the expression to compile, and can lead to poorly optimized access to Linq providers.Here is an answer that directly addresses the original question.