Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PredicateBuilder with VB.NET

I have recreated the Predicatebuilder class in a seperate C# project and I'm trying to use it in a VB.NET project but I keep getting the following error:

Overload resolution failed because no accessible 'Or' accepts this number of arguments.

when I use it like so:

Dim predicate = PredicateBuilder.False(Of t_Quote)()
predicate = predicate.Or(Function(q) q.iQuoteType = iQuoteType)

The relivant project is referenced, I'm using the correct imports statement and it all compiles without any errors.

Any idea where I'm going wrong?

Here is the PredicateBuilder class in C# I'm using:

public static class PredicateBuilder { public static Expression> True() { return f => true; } public static Expression> False() { return f => false; }

    public static Expression<Func<T, bool>> Or<T>(this

Expression> expr1, Expression> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast()); return Expression.Lambda> (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters); }

    public static Expression<Func<T, bool>> And<T>(this

Expression> expr1, Expression> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast()); return Expression.Lambda> (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters); } }

like image 737
rollercoast Avatar asked Jun 21 '26 02:06

rollercoast


2 Answers

Here's the code that works for me in VB.NET, as I also tweaked this class to run in VB.NET...

Imports System.Linq.Expressions

Public Module PredicateBuilder
    Public Function [True](Of T)() As Expression(Of Func(Of T, Boolean))
        Return Function(f) True
    End Function

    Public Function [False](Of T)() As Expression(Of Func(Of T, Boolean))
        Return Function(f) False
    End Function

    <System.Runtime.CompilerServices.Extension()> _
    Public Function [Or](Of T)(ByVal expr1 As Expression(Of Func(Of T, Boolean)), ByVal expr2 As Expression(Of Func(Of T, Boolean))) As Expression(Of Func(Of T, Boolean))
        Dim invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast(Of Expression)())
        Return Expression.Lambda(Of Func(Of T, Boolean))(Expression.[Or](expr1.Body, invokedExpr), expr1.Parameters)
    End Function

    <System.Runtime.CompilerServices.Extension()> _
    Public Function [And](Of T)(ByVal expr1 As Expression(Of Func(Of T, Boolean)), ByVal expr2 As Expression(Of Func(Of T, Boolean))) As Expression(Of Func(Of T, Boolean))
        Dim invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast(Of Expression)())
        Return Expression.Lambda(Of Func(Of T, Boolean))(Expression.[And](expr1.Body, invokedExpr), expr1.Parameters)
    End Function
End Module

And here's how I use it:

Dim pred = PredicateBuilder.True(Of MyClass)()

pred = pred.And(Function(m As MyClass) m.SomeProperty = someValue)
pred = pred.Or(Function(m As MyClass) m.SomeProperty = someValue)
like image 132
Brandon Avatar answered Jun 23 '26 16:06

Brandon


Solved the issue. It was because Option Infer was set to Off. As soon as I set it to On the correct type was infered and it all worked as it should.

like image 20
rollercoast Avatar answered Jun 23 '26 17:06

rollercoast



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!