如何:使用为顺序结果形状映射的存储过程 (LINQ to SQL)

更新:November 2007

这种存储过程可以生成多个结果形状,但您知道结果的返回顺序。请将此方案与您不知道返回顺序的方案作一个对比。有关更多信息,请参见 如何:使用为多个结果形状映射的存储过程 (LINQ to SQL)

示例

下面是一个按顺序返回多个结果形状的存储过程的 T-SQL。

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));
    }

您将使用类似于以下内容的代码来执行此存储过程。

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);
}

请参见

其他资源

存储过程 (LINQ to SQL)