Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在LINQ查询中调用用户定义的函数_Vb.net_Linq - Fatal编程技术网

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

Vb.net 在LINQ查询中调用用户定义的函数,vb.net,linq,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

关于这一点,我在网上看到了一些页面,但不幸的是,示例代码是用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 
它编译得很好,但运行时会挂起。正是返回行完成了此操作。

尝试添加语句:

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

这个问题有点老了,似乎已经解决了。但我仍在发布这篇文章,希望它能帮助其他人找到一个对我有效的替代解决方案

另外请注意,我无法在LINQ查询中使用w/调用我的函数,这使我的情况略有不同。我觉得这更有理由发布另一个答案

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查询中遇到从w/调用非共享用户定义函数的问题时,我使用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 

我对VB不是很在行,但对我来说,在GetProductByType和Title函数定义中似乎缺少了返回类型。请注意,
GetFriendlyTitle
函数应该是
return ConvertedTitle
,而不是
return Title
。不是吗?你说得对。我刚修好,谢谢!我刚刚尝试了这个方法,结果返回“LINQ to Entities无法识别方法'System.String GetFriendlyTitle(System.String)'方法,并且该方法无法转换为存储表达式。”