使用LINQ对DataTable进行排序,当逐列排序可能有所不同时[英] Sorting a DataTable using LINQ, when sort-by-columns may vary

本文是小编为大家收集整理的关于使用LINQ对DataTable进行排序,当逐列排序可能有所不同时的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我需要对数据表进行排序,但是按列排序会有所不同.

场景#1,DataTable1 应该按"Column1"排序.

场景#2,DataTable2 应按"Column1, Column2"排序.

下面是我第一次尝试为此创建一个辅助函数.这工作正常.

Private Sub SortDataTable(ByRef dataTable As DataTable, ByVal sortColumnNames As List(Of String))
    'Validation (not shown here)

    Dim sortOrder = String.Join(", ", sortColumnNames)

    dataTable.DefaultView.Sort = sortOrder
    dataTable = dataTable.DefaultView.Table
End Sub

我尝试在 LINQ 中实现这一点,但是,我不知道如何将多个按列排序的列传递给 lambda 函数.如下所示的工作中代码.

Private Sub SortDataTable(ByRef dataTable As DataTable, ByVal sortColumnNames As List(Of String))
        'Validation (not shown here)

        dataTable.AsEnumerable().OrderBy(Function (row) row(sortColumnNames(0))).ThenBy(...)
    End Sub

我应该如何将多个按列排序传递给 OrderBy/ThenBy 扩展方法?

推荐答案

类似的:

Private Function SortDataTable(table As DataTable, ParamArray columns As String()) As DataTable
    If columns.Length = 0 Then
        Return table
    End If

    firstColumn = columns.First()

    Dim result = table.AsEnumerable().OrderBy(Function(r) r(firstColumn))

    For Each columnName As var In columns.Skip(1)
        result = result.ThenBy(Function(r) r(columnName))
    Next

    Return result.AsDataView().ToTable()

End Function

从这个 C# 代码转换而来(我用 C# 编写了这个,然后使用了 http://www.developerfusion.com/tools/convert/csharp-to-vb/):

DataTable SortDataTable(DataTable table, params string[] columns)
{
    if (columns.Length == 0)
    {
        return table;
    }

    firstColumn = columns.First();

    var result = table.AsEnumerable().OrderBy(r => r[firstColumn]);

    foreach (var columnName in columns.Skip(1))
    {
        result = result.ThenBy(r => r[columnName]);
    }

    return result.AsDataView().ToTable();
}

PS:没有测试过.不过这很简单,应该没问题.

本文地址:https://www.itbaoku.cn/post/1556853.html

问题描述

I need to sort DataTables, however the sort-by-columns vary.

Scenario #1, DataTable1 should be sorted by "Column1".

Scenario #2, DataTable2 should be sorted by "Column1, Column2".

Below is my first attempt at creating a helper function for this purpose. This works ok.

Private Sub SortDataTable(ByRef dataTable As DataTable, ByVal sortColumnNames As List(Of String))
    'Validation (not shown here)

    Dim sortOrder = String.Join(", ", sortColumnNames)

    dataTable.DefaultView.Sort = sortOrder
    dataTable = dataTable.DefaultView.Table
End Sub

I tried implementing this in LINQ, however, I don't know how to pass multiple sort-by-columns to the lambda function. Work-in-progress code shown below.

Private Sub SortDataTable(ByRef dataTable As DataTable, ByVal sortColumnNames As List(Of String))
        'Validation (not shown here)

        dataTable.AsEnumerable().OrderBy(Function (row) row(sortColumnNames(0))).ThenBy(...)
    End Sub

How should I pass multiple sort-by-columns to the OrderBy/ThenBy extension methods?

推荐答案

Something like that:

Private Function SortDataTable(table As DataTable, ParamArray columns As String()) As DataTable
    If columns.Length = 0 Then
        Return table
    End If

    firstColumn = columns.First()

    Dim result = table.AsEnumerable().OrderBy(Function(r) r(firstColumn))

    For Each columnName As var In columns.Skip(1)
        result = result.ThenBy(Function(r) r(columnName))
    Next

    Return result.AsDataView().ToTable()

End Function

Converted from this C# code ( I've written this in C# and then used http://www.developerfusion.com/tools/convert/csharp-to-vb/ ):

DataTable SortDataTable(DataTable table, params string[] columns)
{
    if (columns.Length == 0)
    {
        return table;
    }

    firstColumn = columns.First();

    var result = table.AsEnumerable().OrderBy(r => r[firstColumn]);

    foreach (var columnName in columns.Skip(1))
    {
        result = result.ThenBy(r => r[columnName]);
    }

    return result.AsDataView().ToTable();
}

PS: didn't test that. But that's very simple, so should be no problems.