我在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
所以它的工作原理?
更新:小提琴手輸出使用$過濾
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>
它在瀏覽器中以何種方式失敗? –