我试图在扩展BindingList(Of T)的类中使用LINQ表达式实现多列过滤。以下是相关代码:
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
这是打算像这样使用:
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
但是,在表达式上抛出异常。调用语句。我不知道该提供什么正确的论据。就像现在一样,当我运行代码时,我得到这个错误:
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.
与其尝试使用反射来调用"Equal"或"NotEqual",不如简单地生成一个不同的表达式树。请原谅我可怜的VB。. NET语法,但像这样的东西可能:
Dim comparisonExp As Expression =
If(fieldOperator = "=",
Expression.Equal(objProp, filterValue), _
Expression.NotEqual(objProp, filterValue))