2013-01-17 49 views
0

我在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> 
+0

它在瀏覽器中以何種方式失敗? –

回答

0

這看起來像是WCF數據服務客戶端中的一個錯誤。

乍一看,我沒有看到您提供的Fiddler跟蹤中的任何問題,但我會嘗試在我的最後重現此問題,併爲此提交一個內部錯誤。如果你有一個完整堆棧跟蹤的錯誤,這將是非常有益的。

請注意:LINQ-to-URI轉換位在這裏做的是正確的事情。如果.Where子句僅檢查實體的鍵值,則我們通過直接通過其鍵(例如../vw_mobSurveyor(2))查找實體來優化查詢,而不是使用$filter機制。這將產生一個入口響應有效負載而不是饋送,這與您從Fiddler獲得的相匹配。

爲了回答您的原始問題,您可以強制LINQ翻譯器使用$filter,使謂詞涉及除了實體鍵之外的任何其他內容。例如,你可以這樣做:

.Where(s => s.intSurveyorID == intID && true) 

這顯然是一個黑客,但它應該工作。理想情況下,我們會找到並修復這個bug,然後您可以使用更正常的Where子句:)。

0

它看起來像你所查詢的視圖;你有沒有在實體模型中設置實體關鍵屬性?

+0

是的,它是一個視圖,並且它爲intSurveyorID設置了實體鍵爲true。我會嘗試在桌子上而不是視圖。 –

+0

我現在已經試過這個表,它也有實體鍵集,這也是一樣的。 –

+0

當您在瀏覽器中訪問../vw_mobSurveyor(2)uri時,Fiddler會記錄什麼內容? –