You use LINQ to SQL with stored procedures that return multiple result sets. You want each result set to be returned as strongly typed values. How should you configure the stored procedure function in your data context?

Difficulty: Medium

Correct Answer: Apply the FunctionAttribute and ResultTypeAttribute to the stored procedure function, and use the GetResult<T> method to obtain each strongly typed result set.

Explanation:


Introduction / Context:
LINQ to SQL integrates stored procedures into the data context by mapping them to methods that can be called from code. Some stored procedures return multiple result sets, such as a list of customers followed by a list of orders. To work with these result sets in a type safe way, you must configure attributes on the mapped method and use the appropriate API to retrieve each result as strongly typed entities. Understanding how to use FunctionAttribute, ResultTypeAttribute, and GetResult is essential when working with complex stored procedures in LINQ to SQL.


Given Data / Assumptions:

    The application uses LINQ to SQL with a DataContext that maps to a SQL Server database.

    There is a stored procedure that returns multiple result sets, for example customers and orders.

    You want to map the stored procedure to a method in the DataContext and access each result set as strongly typed entities.

    The question refers to attributes such as FunctionAttribute, ResultTypeAttribute, and ParameterAttribute, and to the GetResult method.

    The goal is to configure the method so that the framework knows the expected types for each result set.


Concept / Approach:
To handle multiple result sets in LINQ to SQL, you map the stored procedure to a method that returns IMultipleResults. You decorate that method with FunctionAttribute to indicate that it is a mapped database function. For each result set type, you apply a ResultTypeAttribute referencing the entity or complex type that represents that result. When you call the method, you get an IMultipleResults object and then call GetResult<T> for each result type in sequence. ParameterAttribute is used for mapping method parameters to stored procedure parameters and is not central to defining return types for multiple result sets.


Step-by-Step Solution:
1. In your DataContext class, declare a method that represents the stored procedure and mark it with FunctionAttribute to map it to the actual database procedure name. 2. Specify the return type of the method as IMultipleResults, indicating that the procedure returns more than one result set. 3. Apply one or more ResultTypeAttribute attributes to the method, each specifying a type that corresponds to one of the result sets returned by the stored procedure. 4. In code, call the stored procedure method on the DataContext to obtain an IMultipleResults instance. 5. Use the GetResult<T> method on the IMultipleResults instance to read each strongly typed result set in order, for example GetResult<Customer>() followed by GetResult<Order>().


Verification / Alternative check:
You can verify correct configuration by running the stored procedure through the mapped method and confirming that the first call to GetResult<Customer> returns a sequence of Customer entities and the next call to GetResult<Order> returns Order entities. If the attributes are not configured correctly, you will encounter runtime errors or untyped results. Microsoft documentation on LINQ to SQL stored procedure mapping describes this pattern using FunctionAttribute, ResultTypeAttribute, and IMultipleResults.


Why Other Options Are Wrong:
Using ParameterAttribute relates to mapping method parameters, not to specifying result types for multiple result sets, so options that rely only on ParameterAttribute cannot provide the required strong typing for results.
Applying ResultTypeAttribute without FunctionAttribute does not fully map the stored procedure method to the database object and may not correctly support IMultipleResults.


Common Pitfalls:
A common mistake is returning a single IEnumerable<T> from a stored procedure that actually produces multiple result sets, which ignores additional data. Another pitfall is forgetting to specify all result types with ResultTypeAttribute, leading to casting errors. Always ensure that the stored procedure method is marked with FunctionAttribute, returns IMultipleResults, and uses ResultTypeAttribute for each expected strongly typed result set.


Final Answer:
You should decorate the stored procedure method with FunctionAttribute and ResultTypeAttribute and then use GetResult<T> to retrieve each strongly typed result set from IMultipleResults.

More Questions from Microsoft Certification

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion