我在 Windows 8 商店应用程序中使用 OData 服务时遇到问题。
如果我这样做:
IEnumerable<vw_mobSurveyor> lstSurveyors =
await ((DataServiceQuery<vw_mobSurveyor>)ODataContext.vw_mobSurveyor
.AddQueryOption("$filter", "intSurveyorID eq " + intID.ToString()))
.ExecuteAsync("GetByID").ConfigureAwait(false);
它有效,URI 的结尾是:/vw_mobSurveyor?$filter=intSurveyorID eq 1
. 如果我在浏览器中尝试,这个 URI 也可以工作,所以一切都很好。
但如果我这样做:
IEnumerable<vw_mobSurveyor> lstSurveyors =
await ((DataServiceQuery<vw_mobSurveyor>)ODataContext.vw_mobSurveyor
.Where(s => s.intSurveyorID == intID))
.ExecuteAsync("GetByID").ConfigureAwait(false);
这会失败并出现客户端内部错误 4。它会为 URI 产生不同的结尾:/vw_mobSurveyor(2)
当我在浏览器中尝试时,此 URI 失败,这就是代码生成它时失败的原因。
我已经阅读了这两个在生成的 URI 中应该是等效的:http: //msdn.microsoft.com/en-us/library/ee622463.aspx#filtering。
因为我想在将来传递一个 Linq 表达式,所以我需要让第二个版本工作。那么如何强制使用第二个版本才能$filter
正常工作呢?
更新:使用 $filter 的工作代码的提琴手输出
HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Length: 1057
Content-Type: application/atom+xml;type=feed;charset=utf-8
Server: Microsoft-IIS/7.0
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 18 Jan 2013 11:35:34 GMT
<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="http://MYSERVER/TESTAPP%20V1/DataService.svc/"
xmlns="http://www.w3.org/2005/Atom"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<id>http://MYSERVER/TESTAPP%20V1/DataService.svc/vw_mobSurveyor</id>
<title type="text">vw_mobSurveyor</title>
<updated>2013-01-18T11:35:34Z</updated>
<link rel="self" title="vw_mobSurveyor" href="vw_mobSurveyor" />
<entry>
<id>http://MYSERVER/TESTAPP%20V1/DataService.svc/vw_mobSurveyor(2)</id>
<category term="SurveyProModel.vw_mobSurveyor" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" title="vw_mobSurveyor" href="vw_mobSurveyor(2)" />
<title />
<updated>2013-01-18T11:35:34Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:intSurveyorID m:type="Edm.Int32">2</d:intSurveyorID>
<d:vchName>Bob Green 2</d:vchName>
<d:vchStatus>Surveyor</d:vchStatus>
</m:properties>
</content>
</entry>
</feed>
更新:失败代码的提琴手输出
HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Length: 824
Content-Type: application/atom+xml;type=entry;charset=utf-8
Server: Microsoft-IIS/7.0
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 18 Jan 2013 11:33:30 GMT
<?xml version="1.0" encoding="utf-8"?>
<entry xml:base="http://MYSERVER/TESTAPP%20V1/DataService.svc/"
xmlns="http://www.w3.org/2005/Atom"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<id>http://MYSERVER/TESTAPP%20V1/DataService.svc/vw_mobSurveyor(2)</id>
<category term="SurveyProModel.vw_mobSurveyor" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" title="vw_mobSurveyor" href="vw_mobSurveyor(2)" />
<title />
<updated>2013-01-18T11:33:30Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:intSurveyorID m:type="Edm.Int32">2</d:intSurveyorID>
<d:vchName>Bob Green 2</d:vchName>
<d:vchStatus>Surveyor</d:vchStatus>
</m:properties>
</content>
</entry>