1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <Extension()>
Public Function Where(Of T)(source As IQueryable(Of T), searchColumn As String,MySearch as String) As IQueryable(Of T)
Dim condPart As Expression = Nothing
Dim ParamExpr = Expression.Parameter(GetType(T), "x")
Dim body = Expression.PropertyOrField(body, searchColumn )
If body.Type.FullName.Contains("Boolean") Then
condPart = Expression.Equal(Expression.Convert(body, GetType(Boolean?)), Expression.Convert(Expression.Constant(CBool(MySearch)), GetType(Boolean?)))
ElseIf body.Type.FullName.Contains("Int32") OrElse body.Type.FullName.Contains("Int64") Then
Dim DblToString = GetType(SqlFunctions).GetMethod("StringConvert", New Type() {GetType(Double?)})
Dim toDouble As Expression = Expression.Call(DblToString, Expression.Convert(body, GetType(Double?)))
condPart = Expression.Call(toDouble, "Contains", Nothing, Expression.Constant(MySearch))
Else
'...
End If
Return source.Provider.CreateQuery(Of T)(Expression.Call(GetType(Queryable), "Where", New Type() {GetType(T)}, source.Expression, Expression.Lambda(condPart , ParamExpr)))
End Function |
Partager