gpt4 book ai didi

asp.net - lambda 函数中未知类型的引用属性

转载 作者:行者123 更新时间:2023-12-04 06:25:53 26 4
gpt4 key购买 nike

我正在尝试创建一个通用函数来从 Telerik 网格过滤器列表构建 Linq 表达式。我试图远离动态 Linq。因为我不知道要为其构建表达式的类型,所以我尝试在 lambda 函数中使用反射来引用属性,但我的结果从未被过滤。调用我的辅助函数以获取属性值从未被调用,尽管它确实命中了“添加”行。将反射放在一条线上(例如下面的“GreaterThan”)并没有什么区别。这是我的代码:

Public Shared Function buildRadFilter(Of T)(ByVal filterExpression As List(Of GridFilterExpression)) As Expressions.Expression(Of Func(Of T, Boolean))
Dim returnPred = PredicateBuilder.True(Of T)()

For Each exp As GridFilterExpression In filterExpression
Dim iExp = exp
Dim doDefault = False
Select Case iExp.FilterFunction
Case "Contains"
returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).Contains(iExp.FieldValue))
Case "DoesNotContain"
returnPred.And(Function(x) Not DirectCast(propVal(x, iExp.FieldName), String).Contains(iExp.FieldValue))
Case "StartsWith"
returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).StartsWith(iExp.FieldValue))
Case "EndsWith"
returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).EndsWith(iExp.FieldValue))
Case "EqualTo"
returnPred.And(Function(x) propVal(x, iExp.FieldName) = iExp.FieldValue)
Case "NotEqualTo"
returnPred.And(Function(x) propVal(x, iExp.FieldName) <> iExp.FieldValue)
Case "GreaterThan"
'returnPred.And(Function(x) propVal(x, iExp.FieldName) > iExp.FieldValue)
returnPred.And(Function(x) x.GetType().GetProperty(iExp.FieldName).GetValue(x, Nothing))
Case "GreaterThanOrEqualTo"
returnPred.And(Function(x) propVal(x, iExp.FieldName) >= iExp.FieldValue)
Case "LessThan"
returnPred.And(Function(x) propVal(x, iExp.FieldName) < iExp.FieldValue)
Case "LessThanOrEqualTo"
returnPred.And(Function(x) propVal(x, iExp.FieldName) <= iExp.FieldValue)
Case "IsEmpty"
returnPred.And(Function(x) propVal(x, iExp.FieldName) = "")
Case "NotIsEmpty"
returnPred.And(Function(x) propVal(x, iExp.FieldName) <> "")
Case "IsNull"
returnPred.And(Function(x) propVal(x, iExp.FieldName) Is Nothing)
Case "NotIsNull"
returnPred.And(Function(x) propVal(x, iExp.FieldName) IsNot Nothing)
Case "Between"
Dim vals As String() = iExp.FieldValue.Split(" ")
If vals.Length > 1 Then
returnPred.And(Function(x) propVal(x, iExp.FieldName) >= vals(0) AndAlso propVal(x, iExp.FieldName) <= vals(1))
Else
doDefault = True
Exit Select
End If
Case "NotBetween"
Dim vals As String() = iExp.FieldValue.Split(" ")
If vals.Length > 1 Then
returnPred.And(Function(x) propVal(x, iExp.FieldName) < vals(0) OrElse propVal(x, iExp.FieldName) > vals(1))
Else
doDefault = True
Exit Select
End If
Case Else
doDefault = True
Exit Select
End Select
If doDefault Then
returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).StartsWith(iExp.FieldValue))
End If
Next

Return returnPred
End Function

'only works for scalar values
Public Shared Function propVal(ByRef obj As Object, ByVal name As String) As Object
Return obj.GetType().GetProperty(name).GetValue(obj, Nothing)
End Function

预先感谢您提供的任何帮助。

最佳答案

原因是And谓词创建一个您不保存的新表达式。它不会就地修改现有的。你需要每returnPred.And切换一次线到

returnPred = returnPred.And(...

关于asp.net - lambda 函数中未知类型的引用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6047816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com