查詢運算式語法範例:分割 (LINQ to Entities)
本主題中的範例將示範如何使用 Skip 和 Take 方法,透過查詢運算式語法來查詢 AdventureWorks Sales Model。 這些範例中使用的 AdventureWorks Sales Model 是從 AdventureWorks 範例資料庫中的 Contact、Address、Product、SalesOrderHeader 和 SalesOrderDetail 資料表所建立。
此主題中的範例將使用下列 using/Imports 陳述式:
Option Explicit On
Option Strict On
Imports L2EExamplesVB.AdventureWorksModel
Imports System.Data.Objects
Imports System.Globalization
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using AdventureWorksModel;
using System.Globalization;
如需詳細資訊,請參閱 HOW TO:在 Visual Studio 中建立 LINQ to Entities 專案。
Skip
範例
下列範例會使用 Skip 方法來取得西雅圖的所有地址 (前兩筆地址除外)。
Using AWEntities As New AdventureWorksEntities
Dim orders As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader
Dim addresses As ObjectQuery(Of Address) = AWEntities.Address
'LINQ to Entities only supports Skip on ordered collections.
Dim query = ( _
From address In addresses _
From order In orders _
Where address.AddressID = order.Address.AddressID _
And address.City = "Seattle" _
Order By order.SalesOrderID _
Select New With _
{ _
.City = address.City, _
.OrderID = order.SalesOrderID, _
.OrderDate = order.OrderDate _
}).Skip(2)
Console.WriteLine("All but first 2 orders in Seattle:")
For Each order In query
Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}", _
order.City, order.OrderID, order.OrderDate)
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Address> addresses = AWEntities.Address;
ObjectQuery<SalesOrderHeader> orders = AWEntities.SalesOrderHeader;
//LINQ to Entities only supports Skip on ordered collections.
var query = (
from address in addresses
from order in orders
where address.AddressID == order.Address.AddressID
&& address.City == "Seattle"
orderby order.SalesOrderID
select new
{
City = address.City,
OrderID = order.SalesOrderID,
OrderDate = order.OrderDate
}).Skip(2);
Console.WriteLine("All but first 2 orders in Seattle:");
foreach (var order in query)
{
Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}",
order.City, order.OrderID, order.OrderDate);
}
Take
範例
下列範例會使用 Take 方法來取得西雅圖的前三筆地址。
Using AWEntities As New AdventureWorksEntities
Dim orders As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader
Dim addresses As ObjectQuery(Of Address) = AWEntities.Address
Dim query = ( _
From address In addresses _
From order In orders _
Where address.AddressID = order.Address.AddressID _
And address.City = "Seattle" _
Select New With _
{ _
.City = address.City, _
.OrderID = order.SalesOrderID, _
.OrderDate = order.OrderDate _
}).Take(3)
Console.WriteLine("First 3 orders in Seattle:")
For Each order In query
Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}", _
order.City, order.OrderID, order.OrderDate)
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Address> addresses = AWEntities.Address;
ObjectQuery<SalesOrderHeader> orders = AWEntities.SalesOrderHeader;
var query = (
from address in addresses
from order in orders
where address.AddressID == order.Address.AddressID
&& address.City == "Seattle"
select new
{
City = address.City,
OrderID = order.SalesOrderID,
OrderDate = order.OrderDate
}).Take(3);
Console.WriteLine("First 3 orders in Seattle:");
foreach (var order in query)
{
Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}",
order.City, order.OrderID, order.OrderDate);
}
}