在LINQ查询中调用用户定义的函数

时间:2013-04-23 13:43:16

标签: vb.net linq

我已经在网上看过几页关于此的内容,但遗憾的是示例代码是在C#中。我无法理解它的大部分内容(和/或通过代码转换器运行它),但我仍然需要帮助使它在VB中运行。

我的自定义功能是:

Public Shared Function GetFriendlyTitle(Title As String) As String
    Dim ConvertedTitle As String = ""
    Try
        Dim oRE As Regex = New Regex("[^a-zA-Z0-9\s]")
        ConvertedTitle = Trim(oRE.Replace(Title, "")).Replace(" ", "_")
    Catch ex As Exception
        LogError(ex)
    End Try
    Return ConvertedTitle
End Function 

这就是我要求退回产品的功能:

Public Shared Function GetProductByTypeAndTitle(URLFriendlyTitle As String, ProductType As ProductType)
    Try
        'don't know why, but the server threw errors if I went c.Type=Type
        Dim pt As Integer = CInt([Enum].Parse(GetType(ProductType), [Enum].GetName(GetType(ProductType), ProductType)))

        Dim context As LionsSharePressEntities = New LionsSharePressModel.LionsSharePressEntities
        return From p In context.Products Where GetFriendlyTitle(p.Title) = URLFriendlyTitle AndAlso p.Type = pt
    Catch ex As Exception
        LogError(ex)
        Return nothing
    End Try
End Function 

它编译得很好,但在我运行它时会挂起。这是回归线。

3 个答案:

答案 0 :(得分:0)

尝试添加Select声明:

return From p In context.Products 
           Where GetFriendlyTitle(p.Title) = URLFriendlyTitle 
           AndAlso p.Type = pt 
           Select p

答案 1 :(得分:0)

答案 2 :(得分:0)

这个问题有点陈旧,似乎已经解决了。但是我仍然会发布这个帖子,希望它可以帮助某些人找到适用于我的替代解决方案。

另请注意,我无法在我的LINQ查询“共享”中调用我的函数,这使我的情况略有不同。我认为更有理由发表替代答案。

Public Function GetFriendlyTitle(Title As String) As String
    Dim ConvertedTitle As String = ""
    Try
        Dim oRE As Regex = New Regex("[^a-zA-Z0-9\s]")
        ConvertedTitle = Trim(oRE.Replace(Title, "")).Replace(" ", "_")
    Catch ex As Exception
        LogError(ex)
    End Try
    Return ConvertedTitle
End Function

当我遇到一个在LINQ查询中调用非共享用户定义函数的问题时,这就是我使用OPs代码片段解决它的方法。

Public Shared Function GetProductByTypeAndTitle(URLFriendlyTitle As String, _
                                                ProductType As ProductType)
    Try
        'don't know why, but the server threw errors if I went c.Type=Type
        Dim pt As Integer = _
                CInt([Enum].Parse(GetType(ProductType), _ 
                     [Enum].GetName(GetType(ProductType), ProductType)))
        Dim context As New LionsSharePressModel.LionsSharePressEntities

        '// Here is the lambda that will call your GetFriendlyTitle function
        Dim callUserFunc As Func(Of String, String) = _
                Function(title As String)
                    Return GetFriendlyTitle(title)
                End Function

        '// Now call Invoke on the lambda and pass in the needed param(s) from within your LINQ query
        return From p In context.Products _
               Where callUserFunc.Invoke(p.Title) = URLFriendlyTitle AndAlso p.Type = pt

        '//return From p In context.Products Where GetFriendlyTitle(p.Title) = URLFriendlyTitle AndAlso p.Type = pt

    Catch ex As Exception

        LogError(ex)
        Return nothing

    End Try
End Function