1
我试图使用Where
方法的集合中此重载方法:为什么我不能在此参数上使用方法?
Private Function getIndexOfObjectById(Of T)(ByVal collection As SortableBindingList(Of T), ByVal id As Integer)
Dim cy = collection.Where(Function(c) c.id = id).FirstOrDefault()
Return collection.IndexOf(cy)
End Function
但我得到一个错误,尽管我知道这个方法存在:
Error 1 Overload resolution failed because no accessible 'Where' can be called with these arguments:
Extension method 'Public Function Where(predicate As System.Func(Of T, Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of T)' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of T, Integer, Boolean)'.
Extension method 'Public Function Where(predicate As System.Func(Of T, Boolean)) As System.Collections.Generic.IEnumerable(Of T)' defined in 'System.Linq.Enumerable': 'id' is not a member of 'T'. \\... 5693 18
有没有不使用强打字的解决方案?我试过了,当我删除键入的参数时,我得到一个运行时异常,声称我的集合没有'Where'方法。 –
如果你不想强打字,你使用的是错误的平台。我的意思是,你可以用动态的方式来完成这个工作,但是这对于边缘情况来说确实是这样。它会导致运行时类型查找性能下降,并且会导致您在运行时显示的错误(如果使用错误类型的参数调用该方法)。 –