How can I dynamically select my Table at runtime with Dynamic LINQ

后端 未结 2 1286
栀梦
栀梦 2020-12-20 09:46

I\'m developing an application to allow engineers to conduct simple single table/view queries against our databases by selecting Database, Table, Fields.

I get how t

2条回答
  •  清歌不尽
    2020-12-20 10:23

    See Get table-data from table-name in LINQ DataContext.

    This is probably actually better done by using direct SQL statements. LINQ will just get in your way.

    VB Conversion:

    NotInheritable Class DataContextExtensions
        Private Sub New()
        End Sub
         _
        Public Shared Function GetTableByName(context As DataContext, tableName As String) As ITable
            If context Is Nothing Then
                Throw New ArgumentNullException("context")
            End If
                If tableName Is Nothing Then
                    Throw New ArgumentNullException("tableName")
                End If
            Return DirectCast(context.[GetType]().GetProperty(tableName).GetValue(context, Nothing), ITable)
        End Function
    End Class
    

    Usage:

    Dim myDataContext as New MyCustomDataContext
    myDataContext.GetTableByName("ORDERS").Where("...")
    

提交回复
热议问题