0

更新了新代码:

我有一些使用实体的非常简单的 Linq

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

我可以在 C# 中做到这一点,只需简单地替换Function(p)p =>. 但是在 VB.NET 中还有一个额外的问题。如果我尝试在 Option Strict ON 的情况下执行上述操作,那么我会收到这个巨大的编译器错误。

我尝试了在Entity Framework Where 方法参数中建议的解决方案,并将我的代码更改为有,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这个做点什么吗?

4

1 回答 1

2

请参阅我的编辑。

尝试这个:

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

认为问题可能是VB无法正确推断出它p总是会是一个Integer,而是猜测它是可以为空的。你也可以试试DirectCast, Convert.ToInt32, 或者CInt像这样:

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

编辑:您正在将 Object 与 Guid 进行比较,因为p可以评估为 Guid 或 0。将零更改为 Guid,您应该很高兴。尝试这个:

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

或者

Return NDCEntity.tbl_Ext_Mobile_PO.Where(Function(p) If(p.AssignedToUID, Nothing) = UserUID)
于 2013-10-08T19:55:26.783 回答