Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the shape of Linq result set

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to realize the shape of Linq result set". In the daily operation, I believe that many people have doubts about how to realize the shape of Linq result set. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to realize the shape of Linq result set". Next, please follow the editor to study!

When a stored procedure can return multiple Linq result set shapes, the return type cannot be strongly typed as a single projection shape. Although LINQ to SQL can generate all possible projection types, it cannot know in what order they will be returned. The ResultTypeAttribute property applies to stored procedures that return multiple result types to specify a collection of types that the procedure can return.

In the following SQL code example, the shape of the Linq result set depends on the input (param1 = 1 or param1 = 2). We don't know which projection to return first.

ALTER PROCEDURE [dbo]. [SingleRowset_MultiShape]-Add the parameters for the stored procedure here (@ param1 int) AS BEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements. SET NOCOUNT ON; if (@ param1 = 1) SELECT * from Customers as c where c.Region = 'WA' else if (@ param1 = 2) SELECT CustomerID, ContactName, CompanyName from Customers as c where c.Region =' WA' END

Drag it into the Ocord R designer, which automatically generates the following code snippet:

[Function (Name= "dbo. [Whole Or Partial Customers Set]")] public ISingleResult Whole_Or_Partial_Customers_Set ([Parameter (DbType= "Int")] System.Nullable param1) {IExecuteResult result = this.ExecuteMethodCall (this, (MethodInfo) (MethodInfo.GetCurrentMethod ()), param1); return ((ISingleResult) (result.ReturnValue));}

However, VS2008 recognizes multiple result set stored procedures as single result set stored procedures, and we manually modify the default generated code to require multiple result sets to be returned, like this:

[Function (Name= "dbo. [Whole Or PartialCustomersSet]")] [ResultType (typeof (WholeCustomersSetResult))] [ResultType (typeof (PartialCustomersSetResult))] public IMultipleResults Whole_Or_Partial_Customers_Set ([Parameter (DbType= "Int")] System.Nullable param1) {IExecuteResult result = this.ExecuteMethodCall (this, (MethodInfo) (MethodInfo.GetCurrentMethod ()), param1); return (IMultipleResults) (result.ReturnValue));}

We define two partial classes respectively to specify the type to be returned. In this way, you can use the following code to call directly to return their respective set of results.

/ / return all Customer result sets IMultipleResults result = db.Whole_Or_Partial_Customers_Set (1); IEnumerable shape1 = result.GetResult (); foreach (WholeCustomersSetResult compName in shape1) {Console.WriteLine (compName.CompanyName);} / / return some Customer result sets result = db.Whole_Or_Partial_Customers_Set (2); IEnumerable shape2 = result.GetResult (); foreach (PartialCustomersSetResult con in shape2) {Console.WriteLine (con.ContactName);}

Statement description: this example uses stored procedures to return a group of customers in the "WA" region. The shape of the Linq result set returned depends on the parameters passed in. If the parameter is equal to 1, all customer properties are returned. If the parameter is equal to 2, the ContactName property is returned.

At this point, the study on "how to realize the shape of Linq result set" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report