Procedura: utilizzare stored procedure mappate per più forme di risultati

Quando una stored procedure consente di restituire più forme di risultati, il tipo restituito non può essere fortemente tipizzato a una singola forma di proiezione. Anche se LINQ to SQL può generare tutti i possibili tipi di proiezione, non può conoscere l'ordine in cui verranno restituiti.

Si consideri questo scenario rispetto a stored procedure che producono più forme di risultati in sequenza. Per altre informazioni, vedere Procedura: utilizzare stored procedure mappate per forme di risultati sequenziali.

L'attributo ResultTypeAttribute viene applicato a stored procedure che restituiscono più tipi di risultati per specificare il set di tipi che la stored procedure può restituire.

Esempio 1

Nell'esempio di codice SQL riportato di seguito la forma dei risultati dipende dall'input (shape =1 o shape = 2). Non si conosce la proiezione che verrà restituita per prima.

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

Esempio 2

Per eseguire questa stored procedure si utilizzerà codice simile al seguente.

Nota

È necessario usare il modello GetResult per ottenere un enumeratore di tipo corretto, in base alla propria conoscenza della stored procedure.

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

Vedi anche