DataServiceQuery<TElement>.AddQueryOption 方法
创建新的 DataServiceQuery<TElement>,并在由返回的查询生成的 URI 中设置查询选项。
命名空间: 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 参数,而 value 则通过对 value 参数调用 ToString 获得。 name 以 $ 开头。
非 WCF 数据服务 语法不以 $ 开头。 非 WCF 数据服务 查询选项可以使用此方法进行添加。 如果选项不是 WCF 数据服务 查询选项,则两次添加同一查询选项是合法的。 如果添加基础 URI 中已存在的查询选项,则将引发异常。
不能使用 AddQueryOption(String, Object) 方法将 $select 查询选项添加到查询 URI 中。 我们建议使用 LINQ Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>) 方法,让客户端在请求 URI 中生成 $select 查询选项。
示例
下面的示例演示与顺序 AddQueryOption 方法调用一起使用的 DataServiceQuery<TElement>,用于仅返回运费成本超过 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);
}