This article is a mirror article of machine translation, please click here to jump to the original article.

View: 16456|Reply: 0

[ASP.NET] Explain the return of the Linq federated query table result set

[Copy link]
Posted on 5/8/2015 4:34:23 PM | | |

In this article, the author introduces Linq joint query in detail, because I am afraid that readers just don't understand this aspect very well, so before talking about Linq joint query, I will first lay some knowledge for you, and then talk about how Linq joint query is implemented, hoping to help you.

First, let's understand some knowledge points of Linq joint query.

1. Anonymous type of delivery



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, and the compiler will not know the actual type of the anonymous type after passing.

This line can be cast through the Cast extension method. Below is a prototype of the Cast method.



public static T Cast(this object o, T t)      
{            
   return ();         
}
2. How to generate an anonymous type list for Linq federated query?        



var User = GetAnonymous(). Cast(new { UserName = "", LastLoginIp = "" });         
var list = new List< ?>();
The principle is the same as the 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 take a look at the MakeList() method:



public static List MakeList(this T t)          {            
    return new List();      
}
Of course, you may think that the above method is not perfect enough, and you need to add a User to the List, so you have the following method:      



public static List MakeList(this T t,params T[] items)         
{            
   return new List(items);   
}  
When called, 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 chase and understand how Linq federated query is 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 only the contents of one table are returned, in this case, a strongly typed List can be returned in the data layer. For example:



public List SelectProducts()   
{      var q = from p in db. Products     
       where p.Supplier.Country == "USA" && p.UnitsInStock == 0        
       select p;        
       return q.ToList;   
}
If the returned result set is more than two tables, how should it be passed? You must have thought that if the result set returned is a single line of data, we can use the anonymous type of delivery we mentioned earlier to get the results we need. public object



public object SelectProducts()  
{      var q = from p in db. Products      
       where p.Supplier.Country == "USA" && p.UnitsInStock == 0        
       select new {p.UnitsInStock,p.Supplier.Sid};      var result = q.Single();  
       return result;   
}
However, this premise is that the business logic layer needs to know the specific types of anonymity types in the data layer. This layering is of little significance. This is not what we want. Moreover, the method of using the anonymous List type to return the result set of multi-row data also failed after experiments. This means that neither of the two methods of passing anonymity at the beginning of this article will work.

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 resolved when the query results are a result set of multiple tables. Since you need to know the type of anonymity returned, you need to define an additional class in addition to not conforming to the multilayer. But this is true and can return the results we need using strong types.

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

Data Layer:



public IEnumerable GetCustomersWithOrders(Func,
TProjection> 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 result returned in this way is still a true anonymous type in the business logic layer and can be used directly.

Method 3: Linq federated queries use stored procedures or views.




Previous:LINQ gets stored procedure return value problem my stored procedure
Next:Microsoft abandoned IE and was determined to do 220,000 lines of code
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com