繁体   English   中英

如何使用AsEnumerable()。OrderBy通过多个条件对DataTable进行排序?

[英]how to Order a DataTable By more than one condition using AsEnumerable().OrderBy?

就像在TSQL中一样:

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

如何使用以下方法对数据表进行排序:

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

您将首先使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM