1

我已经在网上看到了一些关于此的页面,但不幸的是示例代码是 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 

它编译得很好,但是当我运行它时挂起。正是这条返回线做到了。

4

2 回答 2

0

尝试添加Select语句:

return From p In context.Products 
           Where GetFriendlyTitle(p.Title) = URLFriendlyTitle 
           AndAlso p.Type = pt 
           Select p
于 2013-04-23T13:57:32.640 回答
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 
于 2015-07-17T21:55:28.550 回答