选择后在C#中的数据视图排序的前N行 [英] Select top N rows AFTER sorting from Dataview in c#

查看:228
本文介绍了选择后在C#中的数据视图排序的前N行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataTable 10行说,列
中的一个编号为1到10只。
我想对它们进行排序。通常情况下,我这样做:

I have a DataTable with 10 rows say one of the columns numbered 1 to 10 randomly. I want to sort them. usually, I do this:

DataView Dv = new DataView(dtPost, "", "views desc", DataViewRowState.Unchanged);
repeater.DataSource = Dv;
repeater.DataBind();

现在,我只想顶端5行绑定在该数据视图。如果我试试这个:

Now, I just want to bind the top 5 rows in this Dataview. If I try this:

DvPopBlogs.Table.Rows.Cast<System.Data.DataRow>().Take(5);

OR

DvPopBlogs.Table.AsEnumerable().Take(5); //this usually works if sorting wasnt necessary



它的工作原理,但数据视图完全忘掉排序和只需选择前5行。

It works, but the dataView completely forgets about the sorting and just selects 5 rows from top.

我已经与所有DataViewRowStates也尝试过。如何排序后选择前5行?

I have tried it with all DataViewRowStates too. How to select top 5 rows after sorting?

我好像江郎才尽了!
请帮忙!

I seem to run out of ideas! please help!

推荐答案

您正在访问的数据视图,但后来要求在势必 - 表本身并没有进行排序,则数据视图提供该表的排序视图

You are accessing the DataView, but then asking for the Table it is bound to - the table itself isn't sorted, the DataView provides a sorted "view" of the table.

因此,尝试(警告drycode!)

So try (warning drycode!)

DvPopBlogs.DataViewRows.Take(5)

要得到第5(排序顺序)DataViewRows。如果你想在数据行

To get the first 5 (in sort order) DataViewRows. If you want the DataRows:

DvPopBlogs.DataViewRows.Take(5).Select(dvr => dvr.Row)

它很可能从数据视图枚举是DataViewRows收集,所以你可以只使用 DvPopBlogs.Take(5).... 如果你想。

It's quite possible the enumerator from DataView is the DataViewRows collection, so you may be able to just use DvPopBlogs.Take(5).... if you wish.

这篇关于选择后在C#中的数据视图排序的前N行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆