Bonjour,

Je travaille sur une fonction de recherche dynamique.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
Avant une mise à jour de mon VS2017 ça marchait. J'ai corrigé pour pas mal de type. Par exemple, j'ai laissé le type boolean corrigé. Mais je n'arrive pas à corriger pour le type Int32/Int64
Quand je tape 2, je souhaite voir apparaître tous les enregistrements contenant 2 (2;32:102...). C'est pourquoi je transforme mon Champs Body en string.
Mais ça me répond que cette fonction ne peut être utilisée que pour linq to entities. Comme source provient d'une requête linq to SQL...

Merci de votre aide