本文向大家介绍Linq多个结果集,可能好多人还不了解Linq多个结果集,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。
Linq多个结果集
这种存储过程可以生成Linq多个结果集形状,但我们已经知道结果的返回顺序。
下面是一个按顺序返回Linq多个结果集的存储过程Get Customer And Orders。 返回顾客ID为"SEVES"的顾客和他们所有的订单。
- ALTER PROCEDURE [dbo].[Get Customer And Orders]
- (@CustomerID nchar(5))
- -- Add the parameters for the stored procedure here
- AS
- BEGIN
- -- SET NOCOUNT ON added to prevent extra result sets from
- -- interfering with SELECT statements.
- SET NOCOUNT ON;
- SELECT * FROM Customers AS c WHERE c.CustomerID = @CustomerID
- SELECT * FROM Orders AS o WHERE o.CustomerID = @CustomerID
- END
拖到设计器代码如下:
- [Function(Name="dbo.[Get Customer And Orders]")]
- public ISingleResult<Get_Customer_And_OrdersResult>
- Get_Customer_And_Orders([Parameter(Name="CustomerID",
- DbType="NChar(5)")] string customerID)
- {
- IExecuteResult result = this.ExecuteMethodCall(this,
- ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID);
- return ((ISingleResult<Get_Customer_And_OrdersResult>)
- (result.ReturnValue));
- }
同样,我们要修改自动生成的代码:
- [Function(Name="dbo.[Get Customer And Orders]")]
- [ResultType(typeof(CustomerResultSet))]
- [ResultType(typeof(OrdersResultSet))]
- public IMultipleResults Get_Customer_And_Orders
- ([Parameter(Name="CustomerID",DbType="NChar(5)")]
- string customerID)
- {
- IExecuteResult result = this.ExecuteMethodCall(this,
- ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID);
- return ((IMultipleResults)(result.ReturnValue));
- }
同样,自己手写类,让其存储过程返回各自的结果集。
- IMultipleResults result = db.Get_Customer_And_Orders("SEVES");
- //返回Customer结果集
- IEnumerable<CustomerResultSet> customer =
- result.GetResult<CustomerResultSet>();
- //返回Orders结果集
- IEnumerable<OrdersResultSet> orders =
- result.GetResult<OrdersResultSet>();
- //在这里,我们读取CustomerResultSet中的数据
- foreach (CustomerResultSet cust in customer)
- {
- Console.WriteLine(cust.CustomerID);
- }
语句描述:这个实例使用存储过程返回客户“SEVES”及其所有订单。以上介绍Linq多个结果集
【编辑推荐】