WhereNot expression linq

I am trying to create the extension "WhereNot"

Therefore, I can use:

Dim x = "Hello world "
Dim y = x.Split.WhereNot(AddressOf String.IsNullOrEmpty)

Note that my goal is to learn linq expressions; not solve my problem.

I missed 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 Negate function do this?

answers to both vb.net and C # are welcome

+3
source share
2 answers

I understand that this is a very old answer, but I think the selected answer is misleading because the finder was looking for an expression and the selected answer contains lambda.

, IQueryable IEnumerable<T>, IQueryable<T>. Linq.

, .

public static class NegationExpressionHelper
{
    public static IQueryable<T> WhereNot<T>(this IQueryable<T> queryable, Expression<Func<T,bool>> predicate)
    {
        return queryable.Where(predicate.Invert());
    }
    public static Expression<Func<T, bool>> Invert<T>(this Expression<Func<T, bool>> original)
    {
        return Expression.Lambda<Func<T, bool>>(Expression.Not(original.Body), original.Parameters.Single());
    }
}
+10

Negate, , :

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

:

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

WhereNot():

<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
+1

Source: https://habr.com/ru/post/1670425/


All Articles