我已经在网上看到了一些关于此的页面,但不幸的是示例代码是 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
它编译得很好,但是当我运行它时挂起。正是这条返回线做到了。