English 中文(简体)
Pass Func( Telement, TMey) 作为函数参数
原标题:Pass Func(Of TElement, TKey) as a function parameter

我要制作一种 GetAllContacts 方法, 使用Func( 联系人) 类型参数的排序参数, 与 IEO numberable( 联系人) 的排序方法相同。 这里的代码 :

    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, Sort Directction. Asscating) 时,我就会发现错误 :

"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.

我错过了什么?

问题回答

我试过这个(净额4.0):

Public Function tst(a As String) As Integer
        Return a.Length
    End Function

    Public Function GetAllContacts(Of TKey)(ByVal sort As Func(Of String, TKey), ByVal sortDirection As DirectoryServices.SortDirection) As IEnumerable(Of String)
        Return {"a"}
    End Function

并且这一点:

GetAllContacts(AddressOf tst, DirectoryServices.SortDirection.Ascending)
GetAllContacts(Function(a As String) As Integer
                       Return a.Length
                   End Function, DirectoryServices.SortDirection.Ascending)
GetAllContacts(Function(a) a.Length, DirectoryServices.SortDirection.Ascending)

我没有错误

这是一个错误信息错误错误的事例。 我在接口上有一个与执行不同的签名。 一旦我固定了签名, 错误就消失了 。





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签