Reputation: 12684
I'm trying to use the Where
method on the collection in this overloaded method:
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
But I get an error, even though I know the method exists:
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
Upvotes: 1
Views: 399
Reputation: 415600
The compiler has no way to guarantee that your generic type T
will have a property named "id". Therefore, the c.id
expression in your Where()
call is not legal, because that property may not exist.
To fix this, you need to constrain your generic method to some interface that promises an id
integer property will be available, have the method ask for a function that will project your type T
to an id
, or use some other trick that will allow the compiler to know more about your type or the projection of the type to an id integer.
Upvotes: 4