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 return of the result set of Linq joint query table

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to realize the return of the result set of Linq joint query table". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, let's take a look at some of the knowledge points of Linq federated queries.

1. Transmission of anonymous types

Static void Main (string [] args) {var User = GetAnonymous (). Cast (new {UserName = "", LastLoginIp = ""}); Console.Write (User.UserName);} static object GetAnonymous () {var User = new {UserName = "yaosansi", LastLoginIp = "127.0.0.1"}; return User;}

When we define an anonymous type, it can only be passed through the object type, after which the compiler will not be able to know the actual type of the anonymous type.

This line can be cast through the Cast extension method. The following is the prototype of the Cast method.

Public static T Cast (this object o, T t) {return ();}

How do 2.Linq federated queries generate List of anonymous types?

Var User = GetAnonymous () .Cast (new {UserName = "", LastLoginIp = ""}); var list = new List ()

The principle is the same as above.

Var User = new {UserName = "yaosansi", LastLoginIp = "127.0.0.1"}; var list = User.MakeList (); list.Add (User); Console.Write (list [0] .username)

Let's look at the MakeList () method again:

Public static List MakeList (this T t) {return new List ();}

Of course, you may think that the above method is not enough, and you need to Add a User in List, so you have the following method:

Public static List MakeList (this T tje params T [] items) {return new List (items);}

When calling this time, it can be written as:

Var User = new {UserName = "yaosansi", LastLoginIp = "127.0.0.1"}; var list = User.MakeList (User); Console.Write (list [0] .username)

This time let's get to the point and see how Linq federated queries are implemented.

Var Q = from p in db.Products where p.Supplier.Country = = "USA" & & p.UnitsInStock = = 0 select p

The above query is two related tables and returns only the contents of one table, in which case strongly typed List can be returned in the data layer. Such as:

Public List SelectProducts () {var Q = from p in db.Products where p.Supplier.Country = = "USA" & & p.UnitsInStock = = 0 select p; return q.ToList;}

How do you pass it if the returned result set is more than two tables? It must have occurred to you that if you return a result set of single-row data, you can get the results we need by using anonymous type delivery as we mentioned earlier. Public object

Public object SelectProducts () {var Q = from p in db.Products where p.Supplier.Country = = "USA" & & p.UnitsInStock = = 0 select new {p.UnitsInStockp.Supplier.Sid}; var result = q.Single (); return result;}

But this is based on the premise that the business logic layer needs to know the specific types in the anonymous types of the data layer. In this way, layering does not make much sense. This is not what we want. And the method of using anonymous List type to return the result set of multi-row data has also failed in the experiment.

Method 1: Linq federated query customizes classes with the same structure as the return type

Public class CustomQuery {public uint UnitsInStock {get; set;} public int Sid {get; set;}}

This can be solved when the query result is the result set of multiple tables. Because you need to know the anonymous type returned, you need to define an additional class in addition to not conforming to multiple layers. But when it is true, we can use strong typing to return the results we need.

Method 2: Linq federated query uses System.Func delegation (reference: Returning var from a method in C # 3.0)

Data layer:

Public IEnumerable GetCustomersWithOrders (Func projection) {return from customer in _ customers let customerOrders = from order in _ orders where order.CustomerID = customer.ID select projection (customer, customerOrders);}

Business logic layer:

Var results = GetCustomersWithOrders ((customer, orders) = > new {Name = customer.Name, OrderCount = orders.Count ()})

The results returned in this way are still truly anonymous types in the business logic layer and can be used directly.

This is the end of the content of "how to return the result set of Linq joint query table". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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