作法:使用與多個結果型式對應的預存程序
如果預存程序 (Stored Procedure) 可以傳回多個結果圖案,則傳回型別不可以強型別 (Strongly Typed) 為單一投影圖案。 雖然 LINQ to SQL 可以產生所有可能的投影類型,但它無法知道傳回它們的順序。
請將這個案例與循序產生多個結果圖案的預存程序案例比較。 如需詳細資訊,請參閱如何:使用與循序結果型式對應的預存程序。
ResultTypeAttribute 屬性 (Attribute) 會套用至傳回多個結果型別的預存程序,以指定程序可以傳回的型別集。
範例 1
在下列 SQL 程式碼範例中,結果圖案會根據輸入 (shape =1
或 shape = 2
) 而不同。 您無法得知會先傳回哪個投影。
CREATE PROCEDURE VariableResultShapes(@shape int)
AS
if(@shape = 1)
select CustomerID, ContactTitle, CompanyName from customers
else if(@shape = 2)
select OrderID, ShipName from orders
[Function(Name="dbo.VariableResultShapes")]
[ResultType(typeof(VariableResultShapesResult1))]
[ResultType(typeof(VariableResultShapesResult2))]
public IMultipleResults VariableResultShapes([Parameter(DbType="Int")] System.Nullable<int> shape)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), shape);
return ((IMultipleResults)(result.ReturnValue));
}
<FunctionAttribute(Name:="dbo.VariableResultShapes"), _
ResultType(GetType(VariableResultShapesResult1)), _
ResultType(GetType(VariableResultShapesResult2))> _
Public Function VariableResultShapes(<Parameter(DbType:="Int")> ByVal shape As System.Nullable(Of Integer)) As IMultipleResults
Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), shape)
Return CType(result.ReturnValue, IMultipleResults)
End Function
範例 2
您可使用類似下列的程式碼來執行此預存程序。
注意
您必須使用 GetResult 模式,根據您對預存程序的了解以取得正確型別的列舉值。
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
// Assign the results of the procedure with an argument
// of (1) to local variable 'result'.
IMultipleResults result = db.VariableResultShapes(1);
// Iterate through the list and write results (the company names)
// to the console.
foreach(VariableResultShapesResult1 compName in
result.GetResult<VariableResultShapesResult1>())
{
Console.WriteLine(compName.CompanyName);
}
// Pause to view company names; press Enter to continue.
Console.ReadLine();
// Assign the results of the procedure with an argument
// of (2) to local variable 'result'.
IMultipleResults result2 = db.VariableResultShapes(2);
// Iterate through the list and write results (the order IDs)
// to the console.
foreach (VariableResultShapesResult2 ord in
result2.GetResult<VariableResultShapesResult2>())
{
Console.WriteLine(ord.OrderID);
}
Dim db As New Northwnd("c:\northwnd.mdf")
' Assign the results of the procedure with an argument
' of (1) to local variable 'result'.
Dim result As IMultipleResults = db.VariableResultShapes(1)
' Iterate through the list and write results (the company name)
' to the console.
For Each compName As VariableResultShapesResult1 _
In result.GetResult(Of VariableResultShapesResult1)()
Console.WriteLine(compName.CompanyName)
Next
' Pause to view company names; press Enter to continue.
Console.ReadLine()
' Assign the results of the procedure with an argument
' of (2) to local variable 'result.'
Dim result2 As IMultipleResults = db.VariableResultShapes(2)
' Iterate through the list and write results (the order IDs)
' to the console.
For Each ord As VariableResultShapesResult2 _
In result2.GetResult(Of VariableResultShapesResult2)()
Console.WriteLine(ord.OrderID)
Next