2013-10-08 25 views
0

與新代碼更新時間:當Option Strict爲ON時,爲什麼VB.NET中的Entities-to-Linq不像Lambda?

我有一些非常簡單的LINQ中使用實體

Return NDCEntity.tbl_Ext_Mobile_PO.Where(Function(p) If(p.AssignedToUID, 0) = UserUID) 

我能做到這一點在C#中無故障的簡單與p =>更換Function(p)。但在VB.NET中還有一個額外的問題。如果我嘗試使用Option Strict ON進行上述操作,那麼我會得到這個大量的編譯器錯誤。

我試着在Entity Framework Where method parameters上建議的解決方案,並將我的代碼更改爲If(p.AssignedToUID, 0),但這給我一個非常類似的錯誤。

這顯然是某種鑄造錯誤。我已經看到有人在這個網站和其他網站上詢問這個問題的幾個變種,但是我發現的其中一個實際上並沒有答案:你需要做什麼才能使Option Strict ON工作?我更喜歡和它一起工作。

更新: 這裏的錯誤文本:

Error 1 Overload resolution failed because no accessible 'Where' can be called with these arguments: 
'Public Function Where(predicate As String, ParamArray parameters() As System.Data.Objects.ObjectParameter) As System.Data.Objects.ObjectQuery(Of tbl_Ext_Mobile_PO)': Lambda expression cannot be converted to 'String' because 'String' is not a delegate type. 
Extension method 'Public Function Where(predicate As System.Func(Of tbl_Ext_Mobile_PO, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of tbl_Ext_Mobile_PO)' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of tbl_Ext_Mobile_PO, Integer, Boolean)'. 
Extension method 'Public Function Where(predicate As System.Func(Of tbl_Ext_Mobile_PO, Boolean)) As System.Collections.Generic.IEnumerable(Of tbl_Ext_Mobile_PO)' defined in 'System.Linq.Enumerable': Cannot infer a common type, and Option Strict On does not allow 'Object' to be assumed. 
Extension method 'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of tbl_Ext_Mobile_PO, Integer, Boolean))) As System.Linq.IQueryable(Of tbl_Ext_Mobile_PO)' defined in 'System.Linq.Queryable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of tbl_Ext_Mobile_PO, Integer, Boolean)'. 
Extension method 'Public Function Where(predicate As System.Linq.Expressions.Expression(Of System.Func(Of tbl_Ext_Mobile_PO, Boolean))) As System.Linq.IQueryable(Of tbl_Ext_Mobile_PO)' defined in 'System.Linq.Queryable': Cannot infer a common type, and Option Strict On does not allow 'Object' to be assumed. C:\Programming\Sources\NDC\Custom Web Services\Mobile SAP WebServices\1.0.0.0\MobileSAPWebServices\MobileSAPWebServices\SAPMobileWS.asmx.vb 29 20 MobileSAPWebServices 

我應該DirectCast荷蘭國際集團這東西嗎?

+1

粘貼,屏幕截圖是微小的,我們都老了。 – asawyer

+0

可能重複的[實體框架凡方法參數](http://stackoverflow.com/questions/5633334/entity-framework-where-method-parameters) – asawyer

+0

@asawyer哦,這是我沒有找到的問題,讓我看看是否是這樣。謝謝! – cost

回答

2

看我的編輯。

試試這個:

Return NDCEntity.tbl_Ext_Mobile_PO.Where(Function(p) If(p.AssignedToUID.HasValue, p.AssignedToUID.Value, 0) = UserUID) 

認爲問題可能是VB無法正確推斷p總是將是一個Integer,而是猜測它可爲空。您也可以嘗試DirectCastConvert.ToInt32,或CInt像這樣:

Return NDCEntity.tbl_Ext_Mobile_PO.Where(Function(p) DirectCast(If(p.AssignedToUID, 0), Integer) = UserUID) 

編輯:你是比較對象的Guid,因爲p取值爲一個GUID或0.改變零爲GUID,你應該很好去。試試這個:在錯誤和代碼的文本

Return NDCEntity.tbl_Ext_Mobile_PO.Where(Function(p) If(p.AssignedToUID, Guid.Empty) = UserUID) 

OR

Return NDCEntity.tbl_Ext_Mobile_PO.Where(Function(p) If(p.AssignedToUID, Nothing) = UserUID) 
+0

我已經試過了(只需要注意,AssignedToUID是一個Guid,不是Int) ,而我現在得到的是一個不同的錯誤。檢查問題的更新 – cost

相關問題