开发者

LINQ dynamic method call using expressions

开发者 https://www.devze.com 2023-03-02 19:22 出处:网络
I\'m trying to implement multicolumn filtering using LINQ expressions in a class that extends BindingList(Of T).Here is the relevant code:

I'm trying to implement multicolumn filtering using LINQ expressions in a class that extends BindingList(Of T). Here is the relevant code:

Public Function GetFilterPredicate() As Func(Of T, Boolean)
    Dim expressionList As List(Of Expression) = New List(Of Expression)

    For Each item as FilterInfo in _FilterList
        Dim fieldName As String = item.FieldName
        Dim fieldOperator As String = item.FieldOp
        Dim fieldValue As Object = item.FieldValue

        Dim obj As ParameterExpression = Expression.Parameter(GetType(T), "obj")
        Dim objProp As MemberExpression = Expression.PropertyOrField(obj, fieldName)
        Dim filterValue As ConstantExpression = Expression.Constant(fieldValue, objProp.Type)

        Dim methodName As String = If(fieldOperator = "=", "Equal", "NotEqual")

        Dim comparisonExp As MethodCallExpression = Expression.Call( _
            GetType(Expression),
            methodName,
            New Type() {objProp.Type, filterValue.Type},
            objProp, filterValue)

        expressionList.Add(comparisonExp)
    Next

    //
    // combine the expressions in expressionList using Expression.AndAlso
    //
    // create lambda
    //

    Dim fn As Func(Of T, Boolean) = lambda.Compile

    Return fn
End Function

This is intended to be used like so:

Dim source As IQueryable(Of T) = MyBase.Items.ToList.AsQueryable

MyBase.ClearItems()

Dim filterPredicate As Func(Of T, Boolean) = GetFilterPredicate()

For Each item As T In source.Where(开发者_开发技巧filterPredicate)
    MyBase.Items.Add(item)
Next

However, an exception is thrown at the Expression.Call statement. I can't quite figure out the right arguments to supply. As it is now, I am getting this error when I run the code:

InvalidOperationException was unhandled:

No generic method 'Equal' on type 'System.Linq.Expressions.Expression' is
compatible with the supplied type arguments and arguments. No type arguments
should be provided if the method is non-generic.

Any help is appreciated, thanks.


Rather than trying to use reflection to call "Equal" or "NotEqual", you should simply produce a different expression tree. Pardon my poor VB.NET syntax, but something like this maybe:

    Dim comparisonExp As Expression = 
        If(fieldOperator = "=",
           Expression.Equal(objProp, filterValue), _
           Expression.NotEqual(objProp, filterValue))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号