DataRow[] drTest 包含System.Data.DataRow
,比如说,包含 10DataRow
的。在每个 DataRow 内部,我有一个 ItemArray,其中包含Name
and Count
。
现在我想根据 ItemArray 第二条记录计数按降序对 DataRow[] drTest 进行排序。
Example:
DataRow[] drTest contains
At [0] Index - ItemArray[0] - "A"
- ItemArray[1] - 5
At [1] Index - ItemArray[0] - "B"
- ItemArray[1] - 7
我想按降序排列 drTest,以便 drTest[1] 应该出现。
使用 LINQ 的一个好方法是使用LINQPad http://en.wikipedia.org/wiki/LINQPad。这是我编写并测试的一个快速示例来回答您的问题:
var table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Count", typeof(int));
table.Rows.Add("A", 5);
table.Rows.Add("B", 7);
table.Rows.Add("C", 1);
table.Rows.Add("D", 8);
table.Rows.Add("E", 9);
table.Rows.Add("F", 6);
table.Rows.Add("G", 3);
table.Rows.Add("H", 2);
table.Rows.Add("I", 4);
var sorted = table.Rows.Cast<DataRow>()
.OrderByDescending(row => row[1]);
// If you are using LINQPad you can test this using the following line:
sorted.Dump();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)