如何在字符串参数的地方建立动态子句

时间:2019-01-31 15:21:51

标签: vb.net dynamic linq-to-sql

我尝试建立一种方法来向linqToSQL请求添加一个where子句(返回一个可查询的对象)。我尝试了几种方法,但始终使用TOString,Indexof ...,但是此结果是sql请求接受了所有元素以及linq中的过滤器。我在SQL Server提供程序中看到请求。 我想要一种方法来执行此操作,结果是其中包含include

的sql请求

我在带有SQL Server 2016的VisualStudio 2017上工作。我在vb.net中编码

我在linq dynamic library中看到了一件有趣的事情。但是我无法适应我的情况

 <Extension()> _
    Public Function Where(ByVal source As IQueryable, ByVal predicate As String, ByVal ParamArray values() As Object) As IQueryable
        If source Is Nothing Then Throw New ArgumentNullException("source")
        If predicate Is Nothing Then Throw New ArgumentNullException("predicate")
        Dim lambda As LambdaExpression = DynamicExpression.ParseLambda(source.ElementType, GetType(Boolean), predicate, values)
        Return source.Provider.CreateQuery( _
            Expression.Call( _
                GetType(Queryable), "Where", _
                New Type() {source.ElementType}, _
                source.Expression, Expression.Quote(lambda)))
    End Function

但是我不需要所有这些复杂的结构。几年来,我为水电费买单。但是需要升级。这是我的实用程序代码

<Extension()>
Public Function Where(ByVal source As IQueryable, ByVal predicate As String) As IQueryable
    Dim param = Expression.Parameter(GetType(String), "x")
    Return source.Provider.CreateQuery(
            Expression.Call(
                GetType(Queryable), "Where",
                New Type() {source.ElementType},
                source.Expression, Expression.Quote(Expression.Lambda(Expression.Constant(predicate), param))))
End Function


Public Function TFOAppliqueFiltreTri(Of T, MaClassDatas As Class)(Origins As IQueryable(Of T), ByVal MesDonnees As TableFullOption.PagerTabEnCours(Of MaClassDatas)) As iqueriable(of T)
    Dim retour As New TableFullOption.LstRetour

    'Colonne de filtre
    Dim strWh As String = ""
    Dim Filtredrecords As IQueryable(Of T)
    For Each Sort In MesDonnees.MesOptions

                Dim colName = Sort.ColName
                If strWh.Length > 0 Then strWh = strWh & " AND "
                strWh = strWh & String.Format(colName & " like '%{0}%'", Sort.Search)

    Next
    If strWh.Length > 0 Then
        Filtredrecords = Origins.Where(strWh) '<- Here call Where
    Else
        Filtredrecords = Origins
    End If



    Return Filtredrecords 
End Function

我有错误:Aucune方法在“类型”为“ System.Linq.Queryable”的地方,并且兼容的参数类型和参数为整数。 然后我的问题是正确编写lambda表达式。我的谓词参数是:像'%aaa%'这样的Column1。我想重写dynamicLinq的方法在何处接受字符串参数:Column1,例如'%aaa%'

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

最后,在Google上进行了很多阅读并且感觉很少,当然还有很多机会。

 Public Function Where(Of TEntity)(source As IQueryable(Of TEntity), searchColumn As List(Of String), searchValue As String) As IQueryable(Of TEntity)
    Dim cond As Expression = Nothing
    Dim ParamExpr = Expression.Parameter(GetType(TEntity), "x")
    Dim conCat2 = GetType(String).GetMethod("Concat", New Type() {GetType(String), GetType(String)})
    Dim conCat4 = GetType(String).GetMethod("Concat", New Type() {GetType(String), GetType(String), GetType(String), GetType(String)})
    Dim Delim = Expression.Constant("/")
    Dim DateName = GetType(SqlFunctions).GetMethod("DateName", New Type() {GetType(String), GetType(Nullable(Of DateTime))})
    Dim DatePart = GetType(SqlFunctions).GetMethod("DatePart", New Type() {GetType(String), GetType(Nullable(Of DateTime))})
    Dim DblToString = GetType(SqlFunctions).GetMethod("StringConvert", New Type() {GetType(Nullable(Of Double))})

    For Each cn In searchColumn
        For Each colName In cn.Split("|")
            If Not colName.estVide Then
                Dim body As Expression = ParamExpr
                For Each member In colName.Split(".")
                    body = Expression.PropertyOrField(body, member)
                Next
                Dim Tostr As Expression
                If body.Type.FullName.Contains("String") Then
                    Tostr = body
                ElseIf body.Type.FullName.Contains("DateTime") Then
                    Dim day = Expression.Call(Expression.Call(conCat2, Expression.Constant("0"), Expression.Call(DateName, Expression.Constant("day"), body)), "Substring", Nothing, Expression.Constant(0), Expression.Constant(2))
                    Dim Month = Expression.Call(DatePart, Expression.Constant("MM"), body)
                    Dim toDouble = Expression.Convert(Month, GetType(Nullable(Of Double)))
                    Dim mois = Expression.Call(conCat2, Expression.Constant("0"), Expression.Call(Expression.Call(DblToString, toDouble), "Trim", Nothing))
                    Dim an = Expression.Call(DateName, Expression.Constant("year"), body)
                    Tostr = Expression.Call(conCat2, Expression.Call(conCat4, day, Delim, mois, Delim), an)
                Else
                    Tostr = Expression.Call(body, "Convert.ToString", Nothing)
                    'Tostr = Expression.Convert(body, GetType(String))
                End If
                Dim condPart = Expression.Call(Expression.Call(Tostr, "ToLower", Nothing), "Contains", Nothing, Expression.Call(Expression.Constant(searchValue), "ToLower", Nothing))
                If cond Is Nothing Then
                    cond = condPart
                Else
                    cond = Expression.OrElse(cond, condPart)
                End If
            End If
        Next
    Next
    Return source.Provider.CreateQuery(Of TEntity)(Expression.Call(GetType(Queryable), "Where", New Type() {GetType(TEntity)}, source.Expression, Expression.Lambda(cond, ParamExpr)))
End Function

现在,我进行了动态过滤,该过滤器生成了带有complete子句的SQL请求,其中