共用方式為


Scalar queryable example ( Query Design continued ):

 

This post is a part of a series of posts about query design. For the previous post see: https://blogs.msdn.com/vladimirsadov/archive/2007/06/08/generic-queryable-example-query-design-continued.aspx

 

 

Here is one more example of a queryable type. While weird and fairly useless it illustrates that queryable type does not have to be a collection. This one represents a single scalar value.


Public Delegate Function SelectorDelegate(Of T, S)(ByVal arg As T) As S

Class ScalarQueryable(Of T)

    Public element As T

    Public Function [Select](Of S)(ByVal projection As SelectorDelegate(Of T, S)) As ScalarQueryable(Of S)

        Dim new_col As New ScalarQueryable(Of S)

        Dim new_element As S

        new_col.element = new_element

        new_col.element = projection(element)

        Return new_col

    End Function

End Class

Module Module1

    Sub Main()

        Dim col As New ScalarQueryable(Of Integer)

        col.element = 1

        'note that the the result of the query is

        'another ScalarQueryable that contains a string

        Dim q = From i In col Select i_sqr = i * i

        Console.WriteLine(q.element)

    End Sub

End Module