【问题标题】:Generic GetTable from datacontext来自数据上下文的通用 GetTable
【发布时间】:2018-02-13 15:28:47
【问题描述】:

我正在寻找一种方法来创建通用函数/属性以从我的数据上下文中获取表。 现在我有几个属性,每个属性都调用 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

【问题讨论】:

  • GetTable(Of T) 方法呢?

标签: vb.net linq generics


【解决方案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)()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多