0

我很难加入 2 个数据表,结果是加入了数据表。

第一个数据表(标签)保存包括打印机标识的数据。第二个数据表(打印机)保存打印机引用(id > unc)

我想拥有一个数据表作为最终结果(加入),其中包含来自第一个数据表的所有数据以及第二个数据表的字段(unc)。

这就是我一直在尝试的:(注意固定路径是为了方便......)

Sub Main()

    Dim ds1 As new DataSet

    ds1.ReadXml("C:\Program Files (x86)\[COMPANY]\ASW2XML\BICOLOR_a07bfc62-501e-4444-9b6e-3b9d3550e1a4.xml")

    Dim ds2 As New DataSet

    Dim li As string()

    li = IO.File.ReadAllLines("C:\Program Files (x86)\[COMPANY]\ASW2XML\printers.dat")

    Dim printers As New DataTable("Printers")

    printers.Columns.Add("REPRT2")
    printers.Columns.Add("REPRT3")

    For Each s In li.ToList

        Dim dr As DataRow = printers.NewRow

        dr.Item(0) = s.Split("=")(0)
        dr.Item(1) = s.Split("=")(1)

        printers.Rows.Add(dr)

    Next

    printers.AcceptChanges

    Dim labels As DataTable = ds1.Tables(0)

    Dim joined As new DataTable("data")

        'Dim lnq = From label In labels.AsEnumerable Join printer In printers.AsEnumerable On label("REPRT") Equals printer("REPRT2") Select printer
        'Dim lnq = From l In labels Group Join p In printers On l Equals p("REPRT2") Into Group From p In Group Select label = l, ppath = If(p Is Nothing, "(Nothing)", p("REPRT3"))
        Dim lnq = labels.AsEnumerable().Where(Function(o)printers.Select("REPRT2 =" & o.Item("REPRT").ToString).Length = 0)

        joined = lnq.CopyToDataTable


End Sub

感谢您的帮助和启发!

grtz -S-

4

3 回答 3

1

你试过http://msdn.microsoft.com/en-us/library/system.linq.enumerable.join.aspx

将您的数据表作为 IEnumerable 的扩展(就像您所做的那样使用 list ),并且加入 linq 可以使其轻松工作。

然后将连接的表发送到您想要的任何目的地。

于 2012-06-01T20:48:10.203 回答
1

我认为评论会更难理解,所以我把它移到了一个帖子中。

我写了一些结构来帮助你理解 Join 的工作原理:

structure Label
    Public printerId as long
    Public driver as Strring
end structure

structure Printer
    Public unc as string
end structure

如果您将标签和打印机设置为 DataTable(而不是下面的结构),您应该有类似的内容:

 function DoJoin() as datatable 
    'You might remove as datatable in query declaration
    dim query as datatable = Labels.Join(Printers, Function(aLabel) aLabel, _
    function(aPrinter) aPrinter.unc, _
    function(aLabel, aPrinter) New With  
    { .printerID = aLabel.printerId, .driver = aLabel.Driver, _
      .unc = aPrinter.unc
    })
    return query 
end function 

However I wote it this morning in notepad, so you might have to adjust this. I just want to add that you must have the same type of container in order to use join (eg : example in Join at msdn they used 2 Lists.), that are compatible with Linq obects (datatable I do not know).

于 2012-06-04T14:43:37.400 回答
0

我决定以“硬”的方式来做,并遍历父表中的所有行。这对于少量记录来说非常快,我不知道在大量记录上运行它是否会大大降低使用 Linq 解决方案的速度......

这是我正在使用 atm 的代码:

Sub Main()

    Dim ds As new DataSet

    ds.ReadXml("C:\Program Files (x86)\[COMPANY]\ASW2XML\BICOLOR_a07bfc62-501e-4444-9b6e-3b9d3550e1a4.xml")

    Dim labels As DataTable = ds.Tables(0)
    Dim printers As DataTable = GetPrinters

    labels.Columns.Add("REPRT2").SetOrdinal(labels.Columns.IndexOf("REPRT")+1) 'insert new column after key column
    labels.CaseSensitive=False
    labels.AcceptChanges

    For Each dr As DataRow In labels.Rows

        Dim p As String = String.Empty

        Try
            p = printers.Select("ID='" & dr("REPRT") & "'")(0).Item("PATH").ToString
        Catch ex As Exception

        End Try

        dr("REPRT2") = p

    Next

    labels.AcceptChanges

End Sub

Function GetPrinters As DataTable

    Dim printers As New DataTable("Printers")

    Dim li As string()

    li = IO.File.ReadAllLines("C:\Program Files (x86)\[COMPANY]\ASW2XML\printers.dat")

    printers.Columns.Add("ID")
    printers.Columns.Add("PATH")

    For Each s In li.ToList

        Dim dr As DataRow = printers.NewRow

        dr.Item(0) = s.Split("=")(0)
        dr.Item(1) = s.Split("=")(1)

        printers.Rows.Add(dr)

    Next

    printers.AcceptChanges

    Return printers

End Function
于 2012-06-02T09:34:26.427 回答