Multiple associations in one list

In the question below, I am making several unions and selecting them all. I want to return this result to the list, so in this case I will have a list with the number three, if we assume that there are three addresses associated with this order of the identifier of one client ... Now the request works, but I put exp.ToList () this gives me essentially a 2d list (a single-element list in which this element is a list of three-element types. I'm sure there is a good way to do this ... thoughts?

var exp = ( from t in this.reposOrders.All() join p1 in this.reposAddress.All() on t.AddressPrimary equals p1.AddressID into pp1 from p1 in pp1.DefaultIfEmpty() join p2 in this.reposAddress.All() on t.AddressSecondary equals p2.AddressID into pp2 from p2 in pp2.DefaultIfEmpty() join p3 in this.reposAddress.All() on t.AddressThird equals p3.AddressID into pp3 from p3 in pp3.DefaultIfEmpty() where t.CustomerID == customerID select new { p1, p2, p3 } ); 
+4
source share
2 answers

Can you use SelectMany ? There are many linq examples here http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

+2
source

What you want to do suffers from poor normalization; AddressPrimary, AddressSecondary and AddressThird should not be order fields (I accept an order from your line from t in this.reposOrders.All() ), but should be contained in a weak entity or in the connection table.

However, you can get what you want with the following query:

 var primary = from t in this.reposOrders.All() where t.CustomerID == customerID select t.AddressPrimary; var secondary = from t in this.reposOrders.All() where t.CustomerID == customerID select t.AddressSecondary; var tertiary = from t in this.reposOrders.All() where t.CustomerID == customerID select t.AddressThird; var ids = primary.Union(secondary).Union(tertiary); var addresses = from a in this.reposAddress.All() where ids.Contains(a.AddressID) select a; 
0
source

Source: https://habr.com/ru/post/1302279/


All Articles