小編給大家分享一下Linq DataLoadOptions怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
目前創(chuàng)新互聯(lián)公司已為上1000家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)頁空間、網(wǎng)站托管、企業(yè)網(wǎng)站設計、徽縣網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
Linq DataLoadOptions限制
Linq to sql對Linq DataLoadOptions的使用是有限制的,它只支持1個1對多的關(guān)系。一個顧客可能有多個訂單,一個訂單可能有多個詳細訂單:
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>();
這樣的語句執(zhí)行后會導致下面的SQL執(zhí)行N次(參數(shù)不同):
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], (SELECT COUNT(*)
FROM [dbo].[Order Details] AS [t2]
WHERE [t2].[OrderID] = [t0].[OrderID]
) AS [count]
FROM [dbo].[Orders] AS [t0]
LEFT OUTER JOIN [dbo].[Order Details] AS [t1] ON [t1].[OrderID] = [t0].[OrderID]
WHERE [t0].[CustomerID] = @x1
ORDER BY [t0].[OrderID], [t1].[ProductID]
-- @x1: Input StringFixedLength (Size = 5; Prec = 0; Scale = 0) [ALFKI]
而對于多對1的關(guān)系,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>();
由于多個產(chǎn)品對應1個分類,多個詳細訂單對應1個訂單,只有產(chǎn)品和詳細訂單才是多對1的關(guān)系,所以也只會有1次SQL(不過這樣的操作還是少執(zhí)行為妙,消耗太大了)
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]
以上是“Linq DataLoadOptions怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當前標題:LinqDataLoadOptions怎么用
標題網(wǎng)址:http://aaarwkj.com/article34/pegsse.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站制作、虛擬主機、用戶體驗、品牌網(wǎng)站建設、營銷型網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)