我想创建一个 GetAllContacts 方法,它采用 Func(Of Contact, TKey) 类型的排序参数,它与 IEnumerable(Of Contact) 的 OrderBy 方法采用的类型相同。这是我的代码:
Public Function GetAllContacts(Of TKey)(ByVal sort As Func(Of Contact, TKey), ByVal sortDirection As SortDirection) As IEnumerable(Of Contact) Implements IContactRepository.GetAllContacts
Select Case sortDirection
Case sortDirection.Ascending
Return ContactList.OrderBy(sort)
Case sortDirection.Descending
Return ContactList.OrderByDescending(sort)
End Select
End Function
当我调用 GetAllContacts(Func(c) c.ContactID, SortDirection.Ascending) 时,出现错误:
"Value of type 'System.Func(Of Contact, String)' cannot be converted to 'Integer'." on the first parameter
和
"Too many arguments to extension method 'Public Function ElementAtOrDefault(index As Integer) As Contact' defined in 'System.Linq.Enumerable'." on the second parameter.
我错过了什么?