使用linq by column index对vb.net中的Datatable进行排序

时间:2012-03-23 15:26:00

标签: .net vb.net linq

我需要对数据表进行排序。我用linq做这个例子

Dim query = From c In dt.AsEnumerable _
              Order By c.Field(Of DateTime?)("LastPurchaseDate"), _
                       c.Field(Of String)("LastName") Descending
            Dim dv As DataView = query.AsDataView

但我的问题是我需要对字段进行排序,而不是通过传递列的名称,而是传递该字段的索引。有办法吗?

2 个答案:

答案 0 :(得分:3)

Dim dt As New DataTable

Dim orderedby = from p As DataRow in dt.Rows Order by p.Item(0)Ascending,p.Item(1)Ascending Select p

将p转换为datarow:)

答案 1 :(得分:2)

最后,我设法使用数据表的克隆方法。

    Dim DataTableNew As DataTable = New DataTable
    DataTableNew = dt.Clone
    Dim query = (From c In dt.AsEnumerable Order By c.Field(Of String)(12) Ascending)
    query.CopyToDataTable(DataTableNew, LoadOption.OverwriteChanges)