Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement multiple result sets by Linq

Shulou Source: shulou.com Published: 2022-06-02 09:00:04 09月21日 Update

Editor to share with you how to achieve multiple result sets of Linq, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Linq multiple result sets

This stored procedure can generate multiple result set shapes for Linq, but we already know the order in which the results are returned.

The following is a stored procedure Get Customer And Orders that returns multiple result sets of Linq in sequence. Returns customers whose ID is "SEVES" and all their orders.

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

Drag to the designer code as follows:

[Function (Name= "dbo. [Get Customer And Orders]")] public ISingleResult Get_Customer_And_Orders ([Parameter (Name= "CustomerID", DbType= "NChar (5)")] string customerID) {IExecuteResult result = this.ExecuteMethodCall (this, ((MethodInfo) (MethodInfo.GetCurrentMethod ()), customerID); return ((ISingleResult) (result.ReturnValue));}

Again, we want to modify the automatically generated code:

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

Similarly, write your own classes and have their stored procedures return their respective result sets.

IMultipleResults result = db.Get_Customer_And_Orders ("SEVES"); / return Customer result set IEnumerable customer = result.GetResult (); / / return Orders result set IEnumerable orders = result.GetResult (); / / here, we read the data foreach (CustomerResultSet cust in customer) {Console.WriteLine (cust.CustomerID);} in CustomerResultSet

Statement description: this example uses stored procedures to return the customer "SEVES" and all its orders

These are all the contents of the article "how to achieve multiple result sets in Linq". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Results multiple process storage article code content order order customer generation not much most instance customer shape data more knowledge automatic generation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft OPPO Reno MySQL Shulou Technology