2013-04-23 60 views
1

我在網上看到過有關此的幾頁,但不幸的是示例代碼是用C#編寫的。我無法理解它的大部分內容(和/或通過代碼翻譯器運行它),但我仍然需要幫助使其在VB中工作。在LINQ查詢中調用用戶定義的函數

我的自定義函數是:

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 

它編譯罰款,但是當我運行它掛起。這就是那個迴歸線。

+1

我在VB中不強壯,但對我來說,它看起來像你在GetProductByTypeAndTitle函數定義中遺漏了返回類型。 – 2013-04-23 13:52:40

+0

只是爲了提醒你,你的'GetFriendlyTitle'函數應該是'return ConvertedTitle'而不是'return Title'。不應該嗎? – ajakblackgoat 2013-04-23 15:59:50

+0

你說得對。我只是把它修好了,謝謝! – 2013-04-23 23:32:35

回答

0

嘗試添加Select聲明:

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

我只是試過,它回來了「LINQ to Entities不能識別方法'System.String GetFriendlyTitle(System.String)'方法,並且此方法不能被轉換成存儲表達式。」 – 2013-04-23 23:37:36

0

這個問題是有點老了,並似乎已經得到了解決。但是我仍然在發佈這個消息,希望這可能會幫助別人解決問題,併爲我解決問題。

另外請注意,我不能讓我的函數從w /在我的LINQ查詢「Shared」中調用,這使我的情況稍有不同。我認爲更有理由發佈其他答案。

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查詢這個非共享用戶定義函數是我如何解決其使用的OP代碼段,例如,

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 
相關問題