0

我对 WCF 相当陌生。我正在尝试使用 linq 或 lambda 作为分页响应从 WCF 服务检索查询,但没有得到它。我的代码如下:

Dim uri As New Uri(My.Settings.Host)
Dim context As New PMXMigrationEntities(uri)
Dim token As DataServiceQueryContinuation(Of APARTMNT) = Nothing
Dim list As New List(Of APARTMNT)

Try
    ' Execute the query for all apartments and get the response object.'
    Dim response As QueryOperationResponse(Of APARTMNT) = _
        CType(context.APARTMNTs.Execute(), QueryOperationResponse(Of APARTMNT))

    ' With a paged response from the service, use a do...while loop '
    ' to enumerate the results before getting the next link. '
    Do
        ' If nextLink is not null, then there is a new page to load.'
        If token IsNot Nothing Then
            ' Load the new page from the next link URI.'
            response = CType(context.Execute(Of APARTMNT)(token),  _
            QueryOperationResponse(Of APARTMNT))
        End If

        ' Enumerate the apartments in the response.'
        For Each a As APARTMNT In response
            list.Add(a)
        Next

        ' Get the next link, and continue while there is a next link.'
        token = response.GetContinuation()
    Loop While token IsNot Nothing
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try

Return list

我想做类似以下的事情:

CType(context.APARTMNTs.Where(Function(a) a.city.Contains(cityStr)).Execute(), QueryOperationResponse(Of APARTMNT))

和类似的东西:

CType(context.APARTMNTs.Where(Function(a) a.city = cityStr).Execute(), QueryOperationResponse(Of APARTMNT))

现在,我已经读到在这种情况下,linq 或 lambda 被转换为不支持 Contains 函数的 OData 查询,我的替代方案是什么?

4

2 回答 2

1

OData 不支持集合上的通用 Contains 运算符(尽管使用 any/all 不再完全正确)。但是在字符串上(假设 a.city 是一个字符串属性),您可以使用 indexof 代替。例如:

http://services.odata.org/Northwind/Northwind.svc/Customers?$filter=indexof(CompanyName, 'Futt') ne -1

这个查询基本上等同于 CompanyName.Contains("Futt")。在 LINQ 中,这看起来像:

context.Customers.Where(Function(c) c.CompanyName.IndexOf("Futt") <> -1)
于 2013-02-22T11:25:56.700 回答
0

经过长时间的研究和死胡同,我想通了。这是将 DataServiceQuery 转换为 QueryOperatorResponse 的问题

var query = (DataServiceQuery<TENANT>)context.TENANTs.Where(a => a.name.ToLower().Contains("jeff"));

// Execute the query for all tenants and get the response object.
   QueryOperationResponse<TENANT> response =
               (QueryOperationResponse<TENANT>)query.Execute() as QueryOperationResponse<TENANT>;
于 2013-02-22T19:58:00.610 回答