簡體   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