1

I've seen a few pages on the net regarding this, but unfortunately the sample code is in C#. I can't make sense of most of it (and/or run it through a code translator) but I still need help making it work in VB.

My custom function is:

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 

and here's the function I'm calling to return products:

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 

It compiles fine, but hangs when I run it. It's that return line that does it.

4

2 に答える 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 
于 2015-07-17T21:55:28.550 に答える
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 に答える