在运行时已知的字段上加入两个数据 [英] Join two datatables on fields known at runtime

查看:138
本文介绍了在运行时已知的字段上加入两个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单来说:
如何在VS只在运行时知道的字段上加入两个数据表,我一直都知道?
有一个解决方案,如

  Dim result = From t1 In dt,t2 In dt2 _ 
Where t1.Field< DateTime> (timestamp)= t2.Field< DateTime> (samplestamp)
选择t1,t2

这些表的名称只有在运行时,因为这两个表都是动态创建的。



长版本:

在角A中,我有一个Historian服务器,在那里我得到一些数据。该数据表示给定过程中给定时间内创建的所有样本。

在拐角B中,我有一个SQL服务器,我也可以获取一些数据。此数据表示在给定时间内运行的所有批次。



项目需要这2个表格才能显示在图表控件中。



现在有趣的部分。只有在运行时,我才能够告诉我们哪些列可用。我不知道我会收到多少列,因为我无法确定是否有1批运行或100次。我知道的是,我可以收到所有这些数据(我只在运行时看到)可以放入2个数据表(vb.net,因为它是一个旧项目)

我知道对于datatable 1(角A)的实例始终存在于第一列名为timestamp的表中。 (这也是我在这里知道的唯一的列)

我也知道datatable2(转角B)中的第一列被命名为sampletime



所以这里倒数:我可以将给定名称上的这两个数据表连接成一个华丽的数据表,然后我将其用作图表控件的数据源。因此问题解决了。



我已经尝试完成此操作,并在SQL服务器中创建单个sql表,prolem这里是我使用不喜欢事务的服务器,所以没有

解决方案

没有阅读 Longversion ,希望没有忽视的东西: / p>

您可以将这些表传递给两个 DataTables 作为参数的方法。



然后,您可以使用此代码返回可重用的 IEnumerable(OfTuple(Of DataRow,DataRow)):公共函数getSomething(dt1 As DataTable,dt2 As DataTable)As IEnumerable(Of Tuple(Of DataRow,DataRow))$ b($)

加入r2在dt2 
在r1.Field(Of DateTime)(timestamp)等于r2.Field(Of DateTime)(samplestamp)
选择Tuple.Create(r1,r2)
结束函数

匿名类型不是在创建它们的方法之外使用。所以我用这个元组方法。您可以用这种方式评估返回值:

  Dim rowInfos = getSomething(tbl1,tbl2)
对于每个rowInfo in rowInfos
Dim row1 = rowInfo.Item1
Dim row2 = rowInfo.Item2
Dim timestamp = row1.Field(Of DateTime)(timestamp)
Dim samplestamp = row2。字段(DateTime)(samplestamp)
下一个


In a nutshell: How can I join two datatables on fields that VS only knows at runtime and I know all the time? Is there a solution like

    Dim result = From t1 In dt, t2 In dt2 _  
                     Where t1.Field < DateTime > ("timestamp") = t2.Field < DateTime > ("samplestamp")  
                     Select t1, t2

The names of these tables are only known at runtime since both the tables are created dynamically.

Long version:
In Corner A, I have a Historian server where I get some data. This data represents all samples created in a given time for a given proces(ses)
In Corner B i have an SQL server where i also get some data. This data represents all the batches that have run in the given time.

The project needs this 2 tables to be shown in a chart control.

Now for the fun part. Only at runtime am I able to tell wich columns will be available. I don't know how many columns i will receive because i can't tell if 1 batch ran or 100 did. What i do know is that I can receive all this "data" (which i only see at runtime) can be placed into 2 datatables (vb.net because it's an OLD project)
I "Know" for instance that for datatable 1 (corner A) always exists in a table where the first column is named timestamp. (it's also the only column i know here)
And i also know that the first column in datatable2 (corner B) is named sampletime

So here comes the countdown: How am I able to connect these two datatables on the given names into one flashy datatable which I then use as a datasource for my chart control. Hence the problem is solved.

I have tried to accomplish this and create a single sql table in SQL-server, prolem here is that i work with a server that doesn't like transactions so that didn't work out.

解决方案

Without having read the Longversion and hopefully without having overlooked something:

You could pass these tables to a method that takes two DataTables as parameter.

Then you could use this code to return a reusable IEnumerable(Of Tuple(Of DataRow, DataRow)):

Public Function getSomething(dt1 As DataTable, dt2 As DataTable) As IEnumerable(Of Tuple(Of DataRow, DataRow))
    Return From r1 In dt1
           Join r2 In dt2
            On r1.Field(Of DateTime)("timestamp") Equals r2.Field(Of DateTime)("samplestamp")
           Select Tuple.Create(r1, r2)
End Function

Anonymous types are not meant to be used outside of the method where they are created. Hence i've used this tuple approach. You can evaluate the return value in this way:

Dim rowInfos = getSomething(tbl1, tbl2)
For Each rowInfo In rowInfos
    Dim row1 = rowInfo.Item1
    Dim row2 = rowInfo.Item2
    Dim timestamp = row1.Field(Of DateTime)("timestamp")
    Dim samplestamp = row2.Field(Of DateTime)("samplestamp")
Next

这篇关于在运行时已知的字段上加入两个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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