我正在尝试在 LINQ 中使用 SQL 用户定义的函数。我有以下功能:
CREATE FUNCTION [TestSqlFunction] ( @strText VARCHAR(1000) )
RETURNS VARCHAR(1000)
AS
BEGIN
RETURN @strText
END
GO
这导致以下代码:
<DateTimeVerification(DateTimeVerificationState.Verified)> _
<[Function](Name:="dbo.TestSqlFunction")> _
Friend Function TestSqlFunction_Linq(
<Parameter(Name:="strText", DbType:="varchar")>ByVal pstrText As String) As String
Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo),
pstrText)
Return CType(result.ReturnValue, String)
End Function
执行此代码时,它会引发以下异常:
System.InvalidOperationException
'System.String' is not a valid return type for a mapped stored procedure method.
at System.Data.Linq.SqlClient.QueryConverter.TranslateStoredProcedureCall(MethodCallExpression mce, MetaFunction function)
at System.Data.Linq.SqlClient.QueryConverter.VisitMappedFunctionCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.DataContext.ExecuteMethodCall(Object instance, MethodInfo methodInfo, Object[] parameters)
at DataContext.TestSqlFunction_Linq(String pstrText)
它确实适用于以整数作为返回值的函数。异常说“映射存储过程方法”,我认为这很奇怪,因为它是用户定义的函数。有谁知道如何解决这一问题?