DataServiceQuery<TElement>.AddQueryOption 方法
以傳回的查詢所產生之 URI 中設定的查詢選項來建立新的 DataServiceQuery<TElement>。
命名空間: System.Data.Services.Client
組件: Microsoft.Data.Services.Client (在 Microsoft.Data.Services.Client.dll 中)
語法
'宣告
Public Function AddQueryOption ( _
name As String, _
value As Object _
) As DataServiceQuery(Of TElement)
'用途
Dim instance As DataServiceQuery
Dim name As String
Dim value As Object
Dim returnValue As DataServiceQuery(Of TElement)
returnValue = instance.AddQueryOption(name, _
value)
public DataServiceQuery<TElement> AddQueryOption(
string name,
Object value
)
public:
DataServiceQuery<TElement>^ AddQueryOption(
String^ name,
Object^ value
)
member AddQueryOption :
name:string *
value:Object -> DataServiceQuery<'TElement>
public function AddQueryOption(
name : String,
value : Object
) : DataServiceQuery<TElement>
參數
- name
型別:System.String
字串值,包含要加入之查詢字串選項的名稱。
- value
型別:System.Object
物件,包含查詢字串選項的值。
傳回值
型別:System.Data.Services.Client.DataServiceQuery<TElement>
新查詢,其中包含附加到所提供查詢 URI 之要求的查詢選項。
備註
查詢選項會以 ?name=value&name2=value2… 語法的方式加入至做為結果的 URI,其中 name 直接對應至 name 參數,而 value 是透過在 value 參數上呼叫 ToString 而得的。 name 會以 $ 為開頭。
非 WCF Data Services 語法開頭不是 $。 非 WCF Data Services 查詢選項可以使用此方法來加入。 如果此選項不是 WCF Data Services 查詢選項,加入相同的查詢選項兩次是合法的。 如果被加入的查詢已經在基礎 URI 中,則會擲回例外狀況 (Exception)。
您無法使用 AddQueryOption(String, Object) 方法,將 $select 查詢選項加入至查詢 URI。 我們建議您使用 LINQ Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>) 方法,讓用戶端在要求 URI 中產生 $select 查詢選項。
範例
下列範例示範 DataServiceQuery<TElement> 搭配使用循序 AddQueryOption 方法呼叫,只傳回運費超過 $30 美元的訂單,並按出貨日期遞減排序結果。
' 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
// 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);
}
下列範例示範如何撰寫相當於前一個查詢 (使用 AddQueryOption) 的 LINQ 查詢。
' 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
// 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);
}