簡體   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