温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linq DataLoadOptions怎么用

发布时间:2021-12-01 16:50:32 来源:亿速云 阅读:124 作者:小新 栏目:编程语言

小编给大家分享一下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], 
    [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].
    [ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].
    [ShipPostalCode], [t0].[ShipCountry], [t1].[OrderID] AS [OrderID2], [t1].
    [ProductID], [t1].[UnitPrice], [t1].[Quantity], [t1].[Discount], (  

  2. SELECT COUNT(*)  

  3. FROM [dbo].[Order Details] AS [t2]  

  4. WHERE [t2].[OrderID] = [t0].[OrderID]  

  5. ) AS [count]  

  6. FROM [dbo].[Orders] AS [t0]  

  7. LEFT OUTER JOIN [dbo].[Order Details] AS [t1] ON [t1].[OrderID] = [t0].[OrderID]  

  8. WHERE [t0].[CustomerID] = @x1  

  9. ORDER BY [t0].[OrderID], [t1].[ProductID]  

  10. -- @x1: Input StringFixedLength (Size = 5Prec = 0Scale = 0) [ALFKI] 

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

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

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

  1. 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], (  

  2. SELECT COUNT(*)  

  3. FROM [dbo].[Order Details] AS [t5]  

  4. INNER JOIN [dbo].[Orders] AS [t6] ON [t6].[OrderID] = [t5].[OrderID]  

  5. WHERE [t5].[ProductID] = [t0].[ProductID]  

  6. ) AS [count], [t2].[test], [t2].[CategoryID] AS [CategoryID2], [t2].
    [CategoryName], [t2].[Description], [t2].[Picture]  

  7. FROM [dbo].[Products] AS [t0]  

  8. LEFT OUTER JOIN (  

  9. SELECT 1 AS [test], [t1].[CategoryID], [t1].[CategoryName], [t1].
    [Description], [t1].[Picture]  

  10. FROM [dbo].[Categories] AS [t1]  

  11. ) AS [t2] ON [t2].[CategoryID] = [t0].[CategoryID]  

  12. LEFT OUTER JOIN ([dbo].[Order Details] AS [t3]  

  13. INNER JOIN [dbo].[Orders] AS [t4] ON [t4].[OrderID] = [t3].
    [OrderID]) ON [t3].[ProductID] = [t0].[ProductID]  

  14. ORDER BY [t0].[ProductID], [t2].[CategoryID], [t3].[OrderID] 

以上是“Linq DataLoadOptions怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI