使用linq查询迭代数据表

时间:2013-08-03 06:26:37

标签: vb.net linq

我有这些代码:

 Dim dt3 As New DataTable
        dt3.Columns.Add("AdminNo", GetType(String)) '/*Add column AdminNo
        dt3.Columns.Add("PaperNo", GetType(Integer))

        Dim curmodule As String = String.Empty
        For Each dr1 As DataRow In dt1.Rows
            curmodule = dr1("ModuleCode").ToString

            For Each dr2 As DataRow In dt2.Rows
                Dim found As Boolean
                found = False

                For i As Integer = 0 To dt2.Columns.Count - 1

                    If curmodule = dr2(i).ToString Then
                        found = True
                        Dim dr3 As DataRow
                        dr3 = dt3.NewRow
                        dr3("AdminNo") = dr1("AdminNo")
                        dr3("PaperNo") = dr2("PaperNo")
                        dt3.Rows.Add(dr3)
                        'DataGridView3.AutoGenerateColumns = True
                        'Me.DataGridView3.DataSource = dt3
                    End If
                Next
            Next
        Next

        Dim dt As New DataTable
        '' Create 3 typed columns in the DataTable.
        dt.Columns.Add("ConflictingPaper", GetType(String))
        dt.Columns.Add("Numberofstudents", GetType(Integer))
        dt.Columns.Add("AdminNo", GetType(String))

        'Dim query1 = (From a In dt3 Group Convert.ToString(a.Field(Of Integer)("PaperNo")) By AdminNo = (a.Field(Of String)("AdminNo")) Into Group Select dt.LoadDataRow(New Object() {String.Join(":", Group.ToArray()), Group.Count(), AdminNo}, False)).ToList().Count()

        Dim query = From r In dt3.AsEnumerable() Let adminno = r.Field(Of String)("AdminNo") Group r By adminno Into Group Select New With {.AdminNo = adminno, .numberofstudents = Group.Count(), .conflictingpaper = String.Join(":", Group.Select(Function(r) r.Field(Of Integer)("paperno").ToString()).ToArray())}
        'Dim item As string
        For Each Item In query
            dt.LoadDataRow(New Object() {Item.conflictingpaper, Item.numberofstudents, Item.AdminNo}, True)
        Next
        DataGridView3.DataSource = dt
        DataGridView3.AutoGenerateColumns = True

datatable,dt3有数据:

paperno     adminno
1           111411H
1          111380Y
2           182739S
3          111380Y
3          111380Y
3           111411H

我想得到结果:

conflictingpaper   numberofstudents    adminno
1:3                2                   111411H
blank              blank               111380Y
29:32:3            3                      ...
blank             blank                  ...
blank             blank                   ...

我做的linq查询正在填充:

1:3             2             127837Y
32:53           5             103928A
numberofstudents列是错误的,似乎是在计算相互冲突的论文数量。并且没有以我想要的格式显示所有发生在冲突文件中的学生。

有人请帮我解决问题吗?谢谢!

0 个答案:

没有答案