如何使用AsEnumerable()订购DataTable多个条件.QuickBy?

时间:2012-07-29 00:00:58

标签: c# .net vb.net linq datatable

就像在TSQL中一样:

select * from aTable where (aCondition) order by AnIntegerField desc, ADateField 

如何使用以下方式对DataTable进行排序:

dt.AsEnumerable().OrderBy(--two conditions --)

1 个答案:

答案 0 :(得分:4)

您首先使用OrderBy,然后ThenBy(用于提升)或ThenByDescending(用于降序)。

来自Microsoft文档:

    string[] fruits = { "grape", "passionfruit", "banana", "mango", 
                          "orange", "raspberry", "apple", "blueberry" };

    // Sort the strings first by their length and then 
    //alphabetically by passing the identity selector function.
    IEnumerable<string> query =
        fruits.OrderBy(fruit => fruit.Length).ThenBy(fruit => fruit);

或VB(因为问题用两者标记):

    ' Create an array of strings.
    Dim fruits() As String = _
        {"grape", "passionfruit", "banana", "mango", _
         "orange", "raspberry", "apple", "blueberry"}

    ' Sort the strings first by their length and then 
    ' alphabetically by passing the identity function.
    Dim query As IEnumerable(Of String) = _
        fruits _
        .OrderBy(Function(fruit) fruit.Length) _
        .ThenBy(Function(fruit) fruit)