2017-05-15 356 views
0

C#.net,.Net Framework 4.5,VS 2015 我得到DataTable作爲方法的輸入。 需要以特定方式(如下)進行排序:按兩個組中的第一個元素排序。 然後必須返回排序結果作爲DataTable。如何將IEnumerable <IGrouping <int,DataRow >>轉換爲DataTable?

我發現如何排序傳入的DataTable行, 但如何將排序後的記錄轉換爲DataTable?

public DataTable sort(DataTable, dt) 
{ 
    DataTable dt; 

    var v1 = dt.Select() 
     .OrderBy(r => r["TeamId"]) 
     .ThenBy(n => n["Last Name"]) 
     .ThenBy(n => n["First Name"]) 
     .GroupBy(x => index++/2) 
     .OrderBy(row => row.First()["Last Name"]); 

    // need to convert v1 to DataTable sorted_dt - how ? 
    // v1 is type of IOrderedEnumerable<IGrouping<int, DataRow>> 

    return sorted_dt; 
} 

非常感謝

回答

0

我發現以下工作:

sorted_dt =新的DataTable(); sorted_dt = v1.SelectMany(g => g).ToArray()。CopyToDataTable();

相關問題