views:

56

answers:

1

Hello

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

+1  A: 

Yes a Negate method like this will help you:

Public Function Negate(Of T)(ByVal predicate As Func(Of T, Boolean)) As Func(Of T, Boolean)
   Return Function(x) Not predicate(x)
End Function

And then use it like this:

Dim x = "Hello world "
Dim y = x.Split.Where(Negate(Of String)(AddressOf String.IsNullOrEmpty))

Or with a WhereNot() method like this:

<Extension> _
Public Shared Function WhereNot(Of T)(ByVal source As IEnumerable(Of T), ByVal predicate As Func(Of T, Boolean)) As IEnumerable(Of T)
    Return source.Where(Negate(predicate))
End Function
Joel Coehoorn
Thanks.please take a look http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.negate.aspx, tell me if it has to do to our context.
Shimmy