Select 具有特定字段的不同项目在数组中并以表格形式显示

Select distinct items with a specific field in an array and display in tabular form

我有一个返回 3 columns/fields 的存储过程,我将结果集保存在 arrayList 中。每行包含三个字段 ID,name,description.I 想要从这个数组中获取所有不同的类别到单独的数组或一些其他对象中。

例如,如果我的输出有 100 行由 sproc 返回,则可能有 10 行带有类别 1,20 行带有类别 2,35 行带有类别 3 等等。

现在我需要像下面这样显示,即显示每个类别下的所有 ID。

category1
ID Name
1  A
19 B
32 C

category2
ID Name

10  D
11  T
54  D

等等...

我可以使用 gridview 或 Repeater 或 table 来显示它。

示例代码:

 Dim a As ArrayList
 a = //values from sproc
 'we need to implement some logic here display like above.

请告诉我如何正确显示它。提前致谢!

一些嵌套的 linq 查询应该会让你得到那个结果...我已经整理了一个原理的例子,但当然它需要适应你的程序(不能告诉你是什么语言使用你的tags/post,所以这是在vb.net):

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim lstAry As New List(Of String())
    lstAry.Add({"1", "a", "d1"})
    lstAry.Add({"1", "b", "d3"})
    lstAry.Add({"2", "c", "d2"})
    lstAry.Add({"3", "a", "d4"})
    lstAry.Add({"3", "a", "d5"})

    Dim distinctCats = lstAry.Select(Function(x) x(0)).Distinct
    If distinctCats IsNot Nothing Then
        For Each distinctcat As String In distinctCats
            Debug.Print("") 
            Debug.Print(distinctcat)
            Dim catmembers = lstAry.Where(Function(x) (x(0) = distinctcat)).Distinct
            If catmembers IsNot Nothing Then
                For Each catmember As String() In catmembers
                    Debug.Print(catmember(1) & "   " & catmember(2))
                Next
            End If
        Next
    End If
End Sub

这输出:

1
a   d1
b   d3

2
c   d2

3
a   d4
a   d5

因此,如果您 运行 在您的数组列表中使用此子程序的逻辑,您应该会得到格式与您正在寻找的内容非常接近的结果。首先,我获取不同的类别,然后为每个类别获取不同的数组,然后只打印每个组。