如何在 PagedList 上启用排序

发布于 2024-08-12 11:19:15 字数 1070 浏览 4 评论 0 原文

我希望有人使用过来自 Troy Goode 的非常优秀的PagedList?我实际上在 Winforms 应用程序中使用它,虽然它确实有效,但我失去了对它进行排序的能力。

返回 PagedList、控制页面和大小以及绑定到 DataGridView 没有问题,但我最关心的是排序。现在我也遇到了 Muhammad Mosa 的 SortedPageList,但我真的是与参数要求之一混淆。我正在使用私有方法返回 SortedPageList,但下面的代码似乎不起作用:

private SortedPagedList<Location, Location> GetInactiveLocationData(int Index, int Size) {
    sysDataContext ctx = new sysDataContext();
    try {
        var query = ctx.Location.Where(x => x.Active == false).AsQueryable();
        return query.ToPagedList(Index, Size, i => i, false);          
        //return new SortedPagedList<Location, Location>(query, Index, Size, i => i , true);
    }
    catch (Exception) {
        throw;
    }
}

这会引发错误“无法按类型排序:位置”。显然,我想处理用户单击列标题以对该列进行排序的情况。

我知道解决方案涉及的 Lambda 表达式超出了我的知识水平(尽管承认这一点很尴尬),但我在这方面完全一无所知!我非常重视您对上述问题的建议!

感谢你!

Im hoping that someone has used the very excellent PagedList from Troy Goode? Im actually using it in a Winforms app, and while it does work, I have lost the ability to sort it.

Returning a PagedList, controlling the Page and Size, and binding to a DataGridView is no issue, but my biggest concern is Sorting. Now I have also come across the SortedPageList by Muhammad Mosa, but I really am confused with one of the parameter requirements. I am using a private method to return a SortedPageList, but my code below does not seem to work:

private SortedPagedList<Location, Location> GetInactiveLocationData(int Index, int Size) {
    sysDataContext ctx = new sysDataContext();
    try {
        var query = ctx.Location.Where(x => x.Active == false).AsQueryable();
        return query.ToPagedList(Index, Size, i => i, false);          
        //return new SortedPagedList<Location, Location>(query, Index, Size, i => i , true);
    }
    catch (Exception) {
        throw;
    }
}

This throws an error "Cannot order by type: Location". Obvously, I would like to handle the case where the user clciks a column header to sort on that column.

I know that the solution involves Lambda Expressions above a level of knowledge I have (embarrassing as it is to admit) and I am completely clueless on this front! I would really value your advice on the abovementioned!

Thank u!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单爱 2024-08-19 11:19:15

那么,您可以首先学习一些基本 LINQ 操作(向下滚动或此处:LINQ 第 1 部分 -过滤和排序对象列表

一旦你发现了它的魔力,你就会飞起来:P

Well, you can start by learning some basic LINQ operations (scroll down to the "ordering" portion. Or here: LINQ Part 1 - Filtering & Sorting Object Lists .

Once you discover the magic, you'll fly :P

油饼 2024-08-19 11:19:15

好吧,我觉得自己像个白痴!...好吧,有点像!我得到了以下工作:

using (sysContext ctx = new sysDataContext()) {
    try {
        var data = ctx.Location.Where(x => x.Active == false).AsQueryable();
        var query = data.ToPagedList(PageIndex, PageSize, o => o.DueDate, true);
        dgv.DataSource = query;
    }
    catch (Exception) {
        throw;
    }
}

现在,排序表达式在此处描述为 o =>; o.截止日期。这太棒了,虽然这确实有效,但我现在正在尝试找到一种方法来创建一个通用函数,该函数将允许我:

  1. 检测单击了哪个列标题(可能使用 switch 语句)
  2. 将其类型转换为适当的 Linq 实体属性
  3. 通过将该属性转换为通用方法

一种类似于以下内容的方法:

private void RefreshData([type] SortExpression, bool Ascending, int PageIndex, int PageSize) {
    using (sysDataContext ctx = new sysDataContext()) {
        try {
            var data = ctx.Location.AsQueryable();
            var query = data.ToPagedList(PageIndex, PageSize, o => o.[SortExpression], Ascending);
            dgv.DataSource = query;
        }
        catch (Exception) {
            throw;
        }
    }
}

这可能吗?

另外一件事(如果它会帮助你帮助我)SortedPageList 方法看起来像这样:

public static SortedPagedList<T, TResult> ToPagedList<T, TResult>(this IQueryable<T> source, int index, int pageSize, System.Linq.Expressions.Expression<Func<T, TResult>> keySelector, bool asc) {
    return new SortedPagedList<T, TResult>(source, index, pageSize, keySelector, asc);
}

How do I express System.Linq.Expressions.Expression> as a type that可以作为参数传递吗?

OK, I feel like an idiot!...well, somewhat! I got the following to work:

using (sysContext ctx = new sysDataContext()) {
    try {
        var data = ctx.Location.Where(x => x.Active == false).AsQueryable();
        var query = data.ToPagedList(PageIndex, PageSize, o => o.DueDate, true);
        dgv.DataSource = query;
    }
    catch (Exception) {
        throw;
    }
}

Now, the sort expression is described here as o => o.DueDate. This is great, and while this does work, Im trying to now find a way to create a generic function that will allow me:

  1. Detect which column header was clicked (with a switch statement perhaps)
  2. Convert its type into the appropriate Linq Entity property
  3. Pass that property into a generic method

A mehod that looks something like the following:

private void RefreshData([type] SortExpression, bool Ascending, int PageIndex, int PageSize) {
    using (sysDataContext ctx = new sysDataContext()) {
        try {
            var data = ctx.Location.AsQueryable();
            var query = data.ToPagedList(PageIndex, PageSize, o => o.[SortExpression], Ascending);
            dgv.DataSource = query;
        }
        catch (Exception) {
            throw;
        }
    }
}

Is this possible?

One thing additionally (if it will help you help me) the SortedPageList method looks like so:

public static SortedPagedList<T, TResult> ToPagedList<T, TResult>(this IQueryable<T> source, int index, int pageSize, System.Linq.Expressions.Expression<Func<T, TResult>> keySelector, bool asc) {
    return new SortedPagedList<T, TResult>(source, index, pageSize, keySelector, asc);
}

How do I express System.Linq.Expressions.Expression<Func<T, TResult>> as a type that can be passed as a parameter?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文