如何在运行时使用Dynamic LINQ动态选择我的表 [英] How can I dynamically select my Table at runtime with Dynamic LINQ

查看:129
本文介绍了如何在运行时使用Dynamic LINQ动态选择我的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,允许工程师通过选择数据库,表,字段对我们的数据库进行简单的单表/视图查询.

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

我了解了如何使用动态LINQ库样本来在运行时动态选择Select,Where和Order by子句,但是我对如何分配表选择感到困惑.

I get how to use the Dynamic LINQ Library Sample to provide for dynamically selecting the Select, Where and Order by Clauses at runtime but I'm at an impass on how to allot for table choice.

是否有一种方法可以在运行时动态选择"from"表,如果您能提供一些具体示例或将我指向有经验的人的方向?

Is there a way to provide for dynamically selecting the "from" table at run time, and if how could you provide some concrete example or point me in the direction of someone who has?

非常感谢您.

编辑

因此,这两个答案似乎都说明了相同的总体思路.我将尝试将C#转换为VB并使其正常工作.

So Both of the answers seem to be saying the same general Idea. I'm going to try to convert the C# into VB and get it to work.

第一个答案转换为

NotInheritable Class DataContextExtensions
    Private Sub New()
    End Sub
    <System.Runtime.CompilerServices.Extension> _
    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

,但这是向我抛出一个错误,指出只能在模块中定义扩展方法. 但是,当我将其包装在模块标签中时,它仍然会给出相同的错误.

but it's tossing me an Error stating that Extension methods can be defined only in modules. But when I wrap it in module tags it still gives the same error.

因此,我通过将其包装在模块标签中并剥离类标签来进行编译.另外,我可以从中拉出最后一行并将其直接推入允许执行该代码的基本方法中,但是似乎又变回了空.当我尝试枚举结果时,没有结果.不知道这是我的密码问题还是新密码问题,我将进行更多测试.

So I got it to compile by wrapping it in Module Tags and stripping the class tags. Also I can pull the last line out of it and shove that directly into my base method which allows my to execute it but, it seems to be coming back empty. When I try to enumerate the results there are none. Not sure if this is my codes problem or the new codes issue, I'll test more.

这是第二个示例的转换,现在我开始尝试看看是否可以使它们工作.经过测试后,我会再提出问题或结果.

Here's my conversion of the second Example, Now I'm off to try to see if I can get them to work. I'll be back with questions or results after some testing.

'get the table from a type (which corresponds to a table in your context)
Dim dataContextNamespace = "My.DataContext.Namespace"
Dim type = Type.[GetType](dataContextNamespace + tableName)
Dim table = dc.GetTable(type

'add where clauses from a list of them
For Each whereClause As String In whereClauses
    table = table.Where(whereClause)
Next

'generate the select clause from a list of columns
Dim query = table.[Select]([String].Format("new({0})"), [String].Join(",", selectColumns))

感谢您的帮助. BBL

Thanks for the help. BBL

推荐答案

您可以使用 GetTable() 来获取数据的相应ITable.然后结合使用

You can use GetTable() to get the corresponding ITable of your data. Then coupled with using DLINQ, making it relatively easy.

此示例使用 AdventureWorks 数据库.我的项目具有在DatabaseTest.AdventureWorks命名空间的DatabaseTest程序集中定义的上下文.

This example uses the AdventureWorks database. My project has the context defined in the DatabaseTest assembly in the DatabaseTest.AdventureWorks namespace.

'' need my database and DLINQ extensions up top
Imports DatabaseTest.AdventureWorks
Imports System.Linq.Dynamic

'' sample inputs
Dim dc = New AdventureWorksDataContext()
Dim tableName = "Contact"
Dim whereClauses() = {"FirstName = ""John"" OR LastName = ""Smith"""}
Dim selectColumns() = {"FirstName", "LastName"}

'' get the table from a type (which corresponds to a table in your database)
Dim typeName = "DatabaseTest.AdventureWorks." & tableName & ", DatabaseTest"
Dim entityType = Type.GetType(typeName)
Dim table = dc.GetTable(entityType)
Dim query As IQueryable = table

'' add where clauses from a list of them
For Each whereClause As String In whereClauses
    query = query.Where(whereClause)
Next

'' generate the select clause from a list of columns
query = query.Select(String.Format("new({0})", String.Join(",", selectColumns)))

回想起来,使用反射可能是获取表的更简单方法,因为您已经有了名称.但是,然后这些名称可能没有一对一的对应关系,因此您必须对此进行补偿.

In retrospect, using reflection might have been the easier way to get the table since you have the name already. But then the names might not have a 1-to-1 correspondence so you'll have to compensate for it.

Dim table As ITable = dc.GetType().GetProperty(tableName & "s").GetValue(dc, Nothing)

这篇关于如何在运行时使用Dynamic LINQ动态选择我的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆