WhereNot linq expression

萝らか妹 提交于 2019-12-09 19:06:29

问题


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:


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 an IQueryable<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.

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());
    }
}



回答2:


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


来源:https://stackoverflow.com/questions/2264050/wherenot-linq-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!