来自datacontext的通用GetTable

时间:2018-02-13 15:28:47

标签: vb.net linq generics

我正在寻找一种方法来创建一个通用函数/属性来从我的datacontext中获取表。 现在我有几个属性,每个属性调用GetTable函数,但我想将它们加入一个通用函数。

Partial Public Class SAdminDataContext
Inherits DataContext    
Public ReadOnly Property GetCustomer() As Table(Of Customer)
    Get
        Try
            Return GetTable(Of Customer)()
        Catch ex As Exception
            Return Nothing
        End Try

    End Get
End Property
'...

Public ReadOnly Property GetCustomerPerson() As Table(Of CustomerPerson)
    Get
        Try
            Return GetTable(Of CustomerPerson)()
        Catch ex As Exception
            Return Nothing
        End Try
    End Get
End Property End Class

然后我得到这样的数据:

dim datasource = dataContext.GetCustomer

1 个答案:

答案 0 :(得分:1)

你可以包装另一层泛型

Public Function GetIt(Of T)() As List(Of T)
    Dim it As New List(Of T)

    Return it

End Function

并称之为:

Dim l = GetIt(Of String)()

同样的概念应该适用于上述方法。