简体   繁体   English

在linq中进行数据分页而不从数据源中删除记录

[英]Data paging in linq without removing records from the data source

EDIT 编辑

No need for an answer. 不需要答案。 To test how things worked, I removed the .Skip() from the LINQ query and it pages exactly how I expected it to. 为了测试工作.Skip() ,我从LINQ查询中删除了.Skip() ,并按我的预期进行了分页。


My data paging is a bit of a mess. 我的数据分页有点混乱。 When I click on to page 2, rebinding the data source completely removes the first page of data. 当我单击第2页时,重新绑定数据源将完全删除第一页数据。

So with 14 rows returned initially and a page size of 10 rows, I can click on page 2 where I correctly see the remaining 4 rows, but now I can no longer page because the preceding 10 rows have been removed from the data source. 因此,最初返回14行,页面大小为10行,因此我可以单击第2页,在此我可以正确看到剩余的4行,但是现在我无法再进行页面操作,因为之前的10行已从数据源中删除。

Here's my code: 这是我的代码:

private void LoadVouchers(int page)
{
    var ViewModel = new List<ListVouchersViewModel>();
    var PageSize = gvVouchers.PageSize; // Value = 10.
    var Skip = page * PageSize; // Value = 1 * 10 = 10 so skip 10 rows.

    int SupplierId = int.TryParse(hdSupplierId.Value, out SupplierId) ? SupplierId : 0;
    using (ApplicationDbContext Context = new ApplicationDbContext())
    {
        var Vouchers = Context.Vouchers.Where(x => x.SupplierId == SupplierId && !x.Deleted)
                                       .OrderBy(x => x.DateIssued)
                                       .Skip(Skip);

        foreach (var Voucher in Vouchers)
        {
            // Add a new ListVoucherViewModel to the list.
        }
    }
    gvVouchers.DataSource = ViewModel;
    gvVouchers.DataBind();
}

How can I keep the entirety of the data source but show only those that correspond to the selected page? 如何保留整个数据源,但仅显示与所选页面相对应的数据源?

private void LoadVouchers(int page)
{
    var ViewModel = new List<ListVouchersViewModel>();
    var PageSize = gvVouchers.PageSize; // Value = 10.
    var Skip = page * PageSize; // Value = 1 * 10 = 10 so skip 10 rows.

    int SupplierId = int.TryParse(hdSupplierId.Value, out SupplierId) ? SupplierId : 0;
    int totalRowCount = 0;
    using (ApplicationDbContext Context = new ApplicationDbContext())
    {
        totalRowCount = Context.Vouchers.Count();
        var Vouchers = Context.Vouchers.Where(x => x.SupplierId == SupplierId && !x.Deleted)
                                       .OrderBy(x => x.DateIssued)
                                       .Skip(Skip);

        foreach (var Voucher in Vouchers)
        {
            // Add a new ListVoucherViewModel to the list.
        }
    }

    gvVouchers.DataSource = ViewModel;
    gvVouchers.VirtualItemCount = totalRowCount;
    gvVouchers.PageIndex = page;
    gvVouchers.DataBind();
}

I think giving the PagedList library a try is worth a shot. 我认为尝试一下PagedList库值得一试。 It even has a published tutorial on how to page, sort and filter results. 它甚至还发布了有关如何分页,排序和过滤结果的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM