How to: Use Stored Procedures Mapped for Sequential Result Shapes (LINQ to SQL)

This kind of stored procedure can generate more than one result shape, but you know in what order the results are returned. Contrast this scenario with the scenario where you do not know the sequence of the returns. For more information, see How to: Use Stored Procedures Mapped for Multiple Result Shapes (LINQ to SQL).

Example

Here is the T-SQL of a stored procedure that returns multiple result shapes sequentially:

CREATE PROCEDURE MultipleResultTypesSequentially
AS
select * from products
select * from customers
<FunctionAttribute(Name:="dbo.MultipleResultTypesSequentially"), _
ResultType(GetType(MultipleResultTypesSequentiallyResult1)), _
ResultType(GetType(MultipleResultTypesSequentiallyResult2))> _
Public Function MultipleResultTypesSequentially() As IMultipleResults
    Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo))
    Return CType(result.ReturnValue, IMultipleResults)
End Function
    [Function(Name="dbo.MultipleResultTypesSequentially")]
    [ResultType(typeof(MultipleResultTypesSequentiallyResult1))]
    [ResultType(typeof(MultipleResultTypesSequentiallyResult2))]
    public IMultipleResults MultipleResultTypesSequentially()
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
        return ((IMultipleResults)(result.ReturnValue));
    }

You would use code similar to the following to execute this stored procedure.

Dim db As New Northwnd("c:\northwnd.mdf")

Dim sprocResults As IMultipleResults = _
    db.MultipleResultTypesSequentially

' First read products. 
For Each prod As Product In sprocResults.GetResult(Of Product)()
    Console.WriteLine(prod.ProductID)
Next 

' Next read customers. 
For Each cust As Customer In sprocResults.GetResult(Of Customer)()
    Console.WriteLine(cust.CustomerID)
Next
Northwnd db = new Northwnd(@"c:\northwnd.mdf");

IMultipleResults sprocResults =
    db.MultipleResultTypesSequentially();

// First read products. 
foreach (Product prod in sprocResults.GetResult<Product>())
{
    Console.WriteLine(prod.ProductID);
}

// Next read customers. 
foreach (Customer cust in sprocResults.GetResult<Customer>())
{
    Console.WriteLine(cust.CustomerID);
}

See Also

Other Resources

Stored Procedures (LINQ to SQL)