使用LINQ对DataRow []排序 [英] Sort DataRow[] using LINQ
本文介绍了使用LINQ对DataRow []排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataRow [] drTest包含System.Data.DataRow
,例如,包含10个DataRow
.在每个DataRow中,我都有一个包含Name
和Count
的ItemArray.
现在,我要根据ItemArray的第二条记录计数以降序对DataRow [] drTest进行排序.
DataRow[] drTest contains System.Data.DataRow
, say, contains 10 DataRow
's. Inside of each DataRow, I have an ItemArray that contains Name
and Count
.
Now I want to sort the DataRow[] drTest in descending order based on the ItemArray second record count.
示例:
DataRow[] drTest contains
At [0] Index - ItemArray[0] - "A"
- ItemArray[1] - 5
At [1] Index - ItemArray[0] - "B"
- ItemArray[1] - 7
我想按降序排列drTest,以便出现drTest [1].
I want to order drTest in descending order, so that drTest[1] should come up.
推荐答案
使用LINQ的一种很好的方法是使用 LINQPad .这是我编写并测试以回答您的问题的快速示例:
A great way of playing with LINQ is using LINQPad. Here's a quick sample I wrote and tested to answer your question:
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();
这篇关于使用LINQ对DataRow []排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文