开发者

How to enable sorting on a PagedList<T>

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 als开发者_高级运维o 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!


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜