Linq DataLoadOptions描述

Linq DataLoadOptions限制

Linq to sql对Linq DataLoadOptions的使用是有限制的,它只支持1个1对多的关系。一个顾客可能有多个订单,一个订单可能有多个详细订单:

  

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Customer>(c => c.Orders);
options.LoadWith<Order>(o => o.Order_Details);
ctx.LoadOptions = options;
IEnumerable<Customer> customers = ctx.Customers.ToList<Customer>();

 

这样的语句执行后会导致下面的SQL执行N次(参数不同):

   

 1 SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], 
 2 [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].
 3 [ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].
 4 [ShipPostalCode], [t0].[ShipCountry], [t1].[OrderID] AS [OrderID2], [t1].
 5 [ProductID], [t1].[UnitPrice], [t1].[Quantity], [t1].[Discount], (
 6 SELECT COUNT(*)
 7 FROM [dbo].[Order Details] AS [t2]
 8 WHERE [t2].[OrderID] = [t0].[OrderID]
 9 ) AS [count]
10 FROM [dbo].[Orders] AS [t0]
11 LEFT OUTER JOIN [dbo].[Order Details] AS [t1] ON [t1].[OrderID] = [t0].[OrderID]
12 WHERE [t0].[CustomerID] = @x1
13 ORDER BY [t0].[OrderID], [t1].[ProductID]
14 -- @x1: Input StringFixedLength (Size = 5; Prec = 0; Scale = 0) [ALFKI]

 

而对于多对1的关系,Linq to sql对于Linq DataLoadOptions没有限制:

1 DataLoadOptions options = new DataLoadOptions();
2 options.LoadWith<Product>(c => c.Category);
3 options.LoadWith<Product>(c => c.Order_Details);
4 options.LoadWith<Order_Detail>(o => o.Order);
5 ctx.LoadOptions = options;
6 IEnumerable<Product> products = ctx.Products.ToList<Product>();

 

由于多个产品对应1个分类,多个详细订单对应1个订单,只有产品和详细订单才是多对1的关系,所以也只会有1次SQL(不过这样的操作还是少执行为妙,消耗太大了)

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued], [t3].[OrderID], [t3].[ProductID] AS [ProductID2], [t3].[UnitPrice] AS [UnitPrice2], [t3].[Quantity], [t3].[Discount], [t4].[OrderID] AS [OrderID2], [t4].[CustomerID], [t4].[EmployeeID], [t4].[OrderDate], [t4].[RequiredDate], [t4].[ShippedDate], [t4].[ShipVia], [t4].[Freight], [t4].[ShipName], [t4].[ShipAddress], [t4].[ShipCity], [t4].[ShipRegion], [t4].[ShipPostalCode], [t4].[ShipCountry], (  SELECT COUNT(*)  FROM [dbo].[Order Details] AS [t5]  INNER JOIN [dbo].[Orders] AS [t6] ON [t6].[OrderID] = [t5].[OrderID]  WHERE [t5].[ProductID] = [t0].[ProductID]  ) AS [count], [t2].[test], [t2].[CategoryID] AS [CategoryID2], [t2].[CategoryName], [t2].[Description], [t2].[Picture]  FROM [dbo].[Products] AS [t0]  LEFT OUTER JOIN (  SELECT 1 AS [test], [t1].[CategoryID], [t1].[CategoryName], [t1].[Description], [t1].[Picture]  FROM [dbo].[Categories] AS [t1]  ) AS [t2] ON [t2].[CategoryID] = [t0].[CategoryID]  LEFT OUTER JOIN ([dbo].[Order Details] AS [t3]  INNER JOIN [dbo].[Orders] AS [t4] ON [t4].[OrderID] = [t3].[OrderID]) ON [t3].[ProductID] = [t0].[ProductID]  ORDER BY [t0].[ProductID], [t2].[CategoryID], [t3].[OrderID] 

转载于:https://www.cnblogs.com/Peter-Luo/archive/2012/05/31/LinqDataLoadOptions.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值