vb.net 当按列排序可能会有所不同时,使用 LINQ 对数据表进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16691900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:41:41  来源:igfitidea点击:

Sorting a DataTable using LINQ, when sort-by-columns may vary

vb.netlinq

提问by Channs

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

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

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

场景#1,DataTable1 应按“Column1”排序。

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

场景#2,DataTable2 应按“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.

我尝试在 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

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

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

回答by Artur Udod

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/):

从这个 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: didn't test that. But that's very simple, so should be no problems.

PS:没测试过。但这很简单,所以应该没有问题。