Using PredicateBuilder with VB.NET

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

The overload error failed because no available "OR" accepts this number of arguments.

when i use it like this:

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

      

The relational project is referenced, I am using the correct import statement and it compiles without any errors.

Any idea where I am going wrong?

Here is the PredicateBuilder class in C # that I am 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); }}

+2


source to share


4 answers


Solved a problem. This was because the Infer parameter was set to Off. Once I set it to "Inference was made on the correct type and it all worked as it should."



+3


source


Here is the code that works for me in VB.NET, since I also configured 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 this is 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)

      

+7


source


One thing to keep in mind, although it may not solve your specific problem, is case sensitivity when dealing with solutions in different languages.

C # and the CLR itself are case sensitive; VB.NET is not. Depending on how you are consuming the library, an exception might be thrown because the case does not match (and therefore cannot be resolved to a known type).

This can happen if you believe that you have consistently declared namespace names, but one class has a namespace declared with a single uppercase character. It's very easy to do this in Visual Basic, but to VB.NET they all look like they've been compiled into a single, cohesive namespace. As far as the CLR is concerned, but they are two different namespaces.

I've encountered this problem before and it was a very elusive bug to track down.

I know you are consuming a C # project with VB.NET, but watch out for these issues.

+1


source


This works for me:

Dim predicate = PredicateBuilder.False(Of Integer)()
predicate = predicate.Or(Function(q) q Mod 2 = 0)

      

(I don't have your type t_Quote

)

Also, it is a terrible practice to have a type name, eg t_Quote

. It should be called Quote

; Hungarian notation and underscores are both whimpered in C # names.

0


source







All Articles