繁体   English   中英

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

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

编辑

不需要答案。 为了测试工作.Skip() ,我从LINQ查询中删除了.Skip() ,并按我的预期进行了分页。


我的数据分页有点混乱。 当我单击第2页时,重新绑定数据源将完全删除第一页数据。

因此,最初返回14行,页面大小为10行,因此我可以单击第2页,在此我可以正确看到剩余的4行,但是现在我无法再进行页面操作,因为之前的10行已从数据源中删除。

这是我的代码:

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();
}

如何保留整个数据源,但仅显示与所选页面相对应的数据源?

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();
}

我认为尝试一下PagedList库值得一试。 它甚至还发布了有关如何分页,排序和过滤结果的教程

暂无
暂无

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

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