C# 数据集排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11029823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataSet sorting
提问by Jerry Lam
In DataTableI could sorting with
在DataTable我可以排序
dataTable.DefaultView.Sort = "SortField DESC";
I'm getting a DataSetfrom database, I was wondering could I do a sorting on the DataSetlike how I do it in DataTable.
我正在DataSet从数据库中获取一个,我想知道我是否可以DataSet像在DataTable.
采纳答案by Rajesh Subramanian
you can still access the DataTable from the the data set as follows,
您仍然可以从数据集中访问 DataTable,如下所示,
ds.Tables[0].DefaultView.Sort =" criterian";
Hope this helps.
希望这可以帮助。
回答by Darren
DataView view = ds.Tables[0].DefaultView;
view.Sort = "SortField DESC";
http://msdn.microsoft.com/en-us/library/1ay5y4w0(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/1ay5y4w0(v=vs.71).aspx
http://social.msdn.microsoft.com/Forums/nl/netfxbcl/thread/adbd95cd-49d1-483d-b2b2-4b696a66e9a6
http://social.msdn.microsoft.com/Forums/nl/netfxbcl/thread/adbd95cd-49d1-483d-b2b2-4b696a66e9a6
回答by AngeloBad
From tha DataSet object, you can access all the DataTable to intract with.
从 tha DataSet 对象,您可以访问所有要使用的 DataTable。
Try this:
尝试这个:
DataDet.Tables[0].DefaultView.Sort = "sort criteria";
回答by Kishore Kumar
Access the DataTablefrom the the DataSetas follows,
DataTable从DataSet以下访问,
ds.Tables[0].DefaultView.Sort = "SortField DESC";
Hope this helps.
希望这可以帮助。
回答by Taran
DataSet fileTransferDetail = null;//Data to be sorted.
DataSet result = null;//Declare a dataSet to be filled.
//Sort data.
fileTransferDetail.Tables[0].DefaultView.Sort = "ID DESC";
//Store in new Dataset
result.Tables.Add(fileTransferDetail.Tables[0].DefaultView.ToTable());
回答by Balaraju Polaki
Try the following code.
试试下面的代码。
DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
dv.Sort=value;
回答by Zar Shardan
For advanced sorting needs you might want to use LINQ like described here. Basically it allows creating a DataView from a LINQ query using the System.Data.DataTableExtensions.AsDataFiewextension method.
对于高级排序需要你可能想使用LINQ像描述在这里。基本上它允许使用System.Data.DataTableExtensions.AsDataFiew扩展方法从 LINQ 查询创建 DataView 。
Alternatively if you are OK with (or maybe even prefer) using an IEnumerable you could use the System.Data.DataTableExtensions.AsEnumerableextension method. For example:
或者,如果您同意(或者甚至更喜欢)使用 IEnumerable,您可以使用System.Data.DataTableExtensions.AsEnumerable扩展方法。例如:
var enumerable = dataSet.Tables[0].AsEnumerable()
.OrderBy(x => x.Field<string>("ColumnName")
.ThenByDescending(x => x.Field<int?>("OtherColumnName")??0);

