通过下面的查询,我正在进行多个连接并选择所有连接。我想将这个结果返回到一个列表中,所以在这种情况下我会有一个计数为3的List,假设有三个地址与这个单个客户id命令相关联...现在查询有效但是我把exp .ToList()它基本上给了我一个2d列表(一个包含单个元素的列表,其中这个元素是3个元素的类型列表。我确信有一个很好的方法可以做到这一点......想法?
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 }
);
答案 0 :(得分:2)
您可以使用SelectMany吗?这里有许多linq样本http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
答案 1 :(得分:0)
你想要做的事情是正常化不良; AddressPrimary,AddressSecondary和AddressThird不应该是Orders的字段(我假设你的行from t in this.reposOrders.All()
的顺序),但是应该包含在弱实体或连接表中。
也就是说,您可以通过以下查询获得所需内容:
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;