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)
戻り値として整数を持つ関数では機能します。例外には「マップされたストアド プロシージャ メソッド」と書かれていますが、これはユーザー定義関数であるため奇妙だと思います。これを修正する方法を知っている人はいますか?