DataServiceQuery<TElement>.AddQueryOption(String, Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
以傳回的查詢所產生之 URI 中設定的查詢選項來建立新的 DataServiceQuery<TElement>。
public:
System::Data::Services::Client::DataServiceQuery<TElement> ^ AddQueryOption(System::String ^ name, System::Object ^ value);
public System.Data.Services.Client.DataServiceQuery<TElement> AddQueryOption (string name, object value);
member this.AddQueryOption : string * obj -> System.Data.Services.Client.DataServiceQuery<'Element>
Public Function AddQueryOption (name As String, value As Object) As DataServiceQuery(Of TElement)
參數
- name
- String
字串值,包含要加入之查詢字串選項的名稱。
- value
- Object
物件,包含查詢字串選項的值。
傳回
新的查詢,其中包含附加至所提供查詢 URI 的要求查詢選項。
範例
下列範例示範 DataServiceQuery<TElement> 搭配使用循序 AddQueryOption 方法呼叫,只傳回運費超過 $30 美元的訂單,並按出貨日期遞減排序結果。
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
// Define a query for orders with a Freight value greater than 30
// and that is ordered by the ship date, descending.
DataServiceQuery<Order> selectedOrders = context.Orders
.AddQueryOption("$filter", "Freight gt 30")
.AddQueryOption("$orderby", "OrderID desc");
try
{
// Enumerate over the results of the query.
foreach (Order order in selectedOrders)
{
Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}",
order.OrderID, order.ShippedDate, order.Freight);
}
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred during query execution.", ex);
}
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
' Define a query for orders with a Freight value greater than 30
' and that is ordered by the ship date, descending.
Dim selectedOrders As DataServiceQuery(Of Order) = context.Orders _
.AddQueryOption("$filter", "Freight gt 30") _
.AddQueryOption("$orderby", "OrderID desc")
Try
' Enumerate over the results of the query.
For Each order As Order In selectedOrders
Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}", _
order.OrderID, order.ShippedDate, order.Freight)
Next
Catch ex As DataServiceQueryException
Throw New ApplicationException( _
"An error occurred during query execution.", ex)
End Try
下列範例示範如何撰寫相當於前一個查詢 (使用 AddQueryOption) 的 LINQ 查詢。
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
// Define a query for orders with a Freight value greater than 30
// and that is ordered by the ship date, descending.
var selectedOrders = from o in context.Orders
where o.Freight > 30
orderby o.ShippedDate descending
select o;
try
{
// Enumerate over the results of the query.
foreach (Order order in selectedOrders)
{
Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}",
order.OrderID, order.ShippedDate, order.Freight);
}
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred during query execution.", ex);
}
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
' Define a query for orders with a Freight value greater than 30
' and that is ordered by the ship date, descending.
Dim selectedOrders = From o In context.Orders _
Where (o.Freight > 30) _
Order By o.ShippedDate Descending _
Select o
Try
' Enumerate over the results of the query.
For Each order As Order In selectedOrders
Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}", _
order.OrderID, order.ShippedDate, order.Freight)
Next
Catch ex As DataServiceQueryException
Throw New ApplicationException( _
"An error occurred during query execution.", ex)
End Try
備註
查詢選項會使用 ?name=value&name2=value2
... 新增至結果 URI 語法,其中名稱會直接對應至 name
參數, value
並在 參數上 value
呼叫ToString來取得 。
name
會以 $
為開頭。
非 WCF Data Services 語法不是以 $
開頭。 您可以使用此方法來新增非 WCF Data Services 查詢選項。 如果選項不是 WCF Data Services 查詢選項,則新增相同的查詢選項兩次是合法的。 如果被加入的查詢已經在基礎 URI 中,則會擲回例外狀況 (Exception)。