WCF 数据服务使您能够从基于 .NET Framework 的客户端应用程序查询数据服务,方法是使用生成的客户端数据服务类。这样做最简单的方法是撰写语言集成查询 (LINQ) 的查询表达式,其中包含所需的查询选项。 还可以调用一系列 LINQ 查询方法来编写等效的查询。 最后,可以使用 AddQueryOption 方法向查询添加查询选项。 在上述每种情况下,客户端生成的 URI 都包含应用了选定查询选项的被请求实体集。 有关更多信息,请参见查询数据服务(WCF 数据服务)。
本主题中的示例使用 Northwind 示例数据服务和自动生成的客户端数据服务类。 此服务和这些客户端数据类是在完成 WCF 数据服务快速入门时创建的。
示例
下面的示例演示如何编写 LINQ 查询表达式,该表达式仅返回运费成本超过 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 = 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);
}
下面的示例演示如何使用 LINQ 查询方法编写等效于上述查询的 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 = context.Orders _
.Where(Function(o) o.Freight.Value > 30) _
.OrderByDescending(Function(o) o.ShippedDate)
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 = context.Orders
.Where(o => o.Freight > 30)
.OrderByDescending(o => o.ShippedDate);
try
{
// Enumerate over the results of the query.
foreach (Order currentOrder in selectedOrders)
{
Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}",
currentOrder.OrderID, currentOrder.ShippedDate,
currentOrder.Freight);
}
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred during query execution.", ex);
}
下面的示例演示如何使用 AddQueryOption 方法创建等效于上述示例的 DataServiceQuery<TElement>。
' 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);
}
下面的示例演示如何使用 $orderby 查询选项按运费属性对返回的订单对象进行筛选和排序。
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
' Define a query for orders with a Freight value greater than 30
' that also orders the result by the Freight value, descending.
Dim selectedOrders As DataServiceQuery(Of Order) = _
context.Orders.AddQueryOption("$orderby", "Freight gt 30 desc")
Try
' Enumerate over the results of the query.
For Each order As Order In selectedOrders
Console.WriteLine("Order ID: {0} - Freight: {1}", _
order.OrderID, 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
// that also orders the result by the Freight value, descending.
DataServiceQuery<Order> selectedOrders = context.Orders
.AddQueryOption("$orderby", "Freight gt 30 desc");
try
{
// Enumerate over the results of the query.
foreach (Order order in selectedOrders)
{
Console.WriteLine("Order ID: {0} - Freight: {1}",
order.OrderID, order.Freight);
}
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred during query execution.", ex);
}