来自datacontext的通用GetTable [英] Generic GetTable from datacontext
本文介绍了来自datacontext的通用GetTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种方法来创建一个通用函数/属性来从我的datacontext中获取表。
现在我有几个属性可以调用GetTable函数,但我想将它们加入到一个泛型函数中。
部分公共类SAdminDataContext
继承DataContext
公共ReadOnly属性GetCustomer()作为表(客户)
获取
尝试
返回GetTable(Of Customer)()
Catch ex例外
返回Nothing
结束尝试
结束获取
结束属性
'...
Public ReadOnly Property GetCustomerPerson()作为Table(Of CustomerPerson)
获取
尝试
返回GetTable(Of CustomerPerson)()
作为例外捕获例外
返回无
结束尝试
结束获取
End Property End Class
和那么我得到这样的数据:
dim datasource = dataContext.GetCustomer
解决你可以换成另一层泛型
Public Function GetIt(Of T)()As List of(T)
Dim it As New List(Of T)
返回
结束函数
然后调用它:
Dim l = GetIt (Of String)()
相同的概念应该适用于上述方法。 b $ b
I'm looking for a way to create a generic function/property to get the table from my datacontext. Now i have several properties that each call the GetTable function, but i would like to join them in one generic function.
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
and then i get the data like this:
dim datasource = dataContext.GetCustomer
解决方案
You could wrap in another layer of generics
Public Function GetIt(Of T)() As List(Of T)
Dim it As New List(Of T)
Return it
End Function
And to call it:
Dim l = GetIt(Of String)()
The same concept should work for the above approach.
这篇关于来自datacontext的通用GetTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文