Share via


방법: 여러 결과 도형에 매핑된 저장 프로시저 사용(LINQ to SQL)

업데이트: November 2007

저장 프로시저에서 여러 결과 도형을 반환할 수 있는 경우 반환 형식은 단일 프로젝션 도형에 대해 강력하게 형식화될 수 없습니다. LINQ to SQL에서는 모든 가능한 프로젝션 형식을 생성할 수 있더라도 해당 형식들이 반환되는 순서는 알 수는 없습니다.

이 시나리오와 달리 순차적으로 여러 결과 도형을 생성하는 저장 프로시저에 대한 자세한 내용은 방법: 순차 결과 도형에 매핑된 저장 프로시저 사용(LINQ to SQL)을 참조하십시오.

ResultTypeAttribute 특성을 여러 결과 형식을 반환하는 저장 프로시저에 적용하여 프로시저에서 반환할 수 있는 형식 집합을 지정합니다.

예제

다음 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
<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
 [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));
    }

다음과 유사한 코드를 사용하여 이 저장 프로시저를 실행할 수 있습니다.

참고:

저장 프로시저에 대한 정보에 따라 GetResult<TElement> 패턴을 사용하여 올바른 형식의 열거자를 가져와야 합니다.

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

참고 항목

기타 리소스

저장 프로시저(LINQ to SQL)