业务层中的LINQ分页/用户层中的gridview排序

发布于 2024-10-12 18:58:12 字数 3501 浏览 3 评论 0原文

我的用户层中有一个 gridview,它使用业务层方法作为其数据源,并且我希望 gridview 支持分页和排序。当我从该方法返回一个 Ienumerable 时,它​​将带回所有数据。如果我使用 Take/Skip 只返回一页的数据,则 gridview 不会意识到有很多页的数据。

当我将 Ienumerable 更改为 IQueryable 时,DataBind() 失败,因为数据已被处理。我认为这个问题与查询实际执行的时间有关,并且可能与我应用于查询的过滤器有关(见下文)。

用户层代码:

grdSelectedQuestionaires.DataSource = assessment.FilteredAssessmentList(filter);
grdSelectedQuestionaires.DataBind(); <-- Fails - "Cannot access a disposed object"

业务层代码:

public IQueryable<Assessment> FilteredAssessmentList(AssessmentSearchFilter filter)
    {
        using (PAQcDataLayerDataContext dc = new PAQcDataLayerDataContext())
        {
            var rows = (
                    from a in dc.vPAQSummaries
                    select new Assessment()
                    {
                        PAQNumber = a.PAQNumber.Trim(),
                        CustomerID = (int)a.CustomerID,
                        Department = a.Department.Trim(),
                        CustomerName = a.CustomerName.Trim(),
                        DOTNumber = a.DOTNumber.Trim(),
                        OrgName = a.OrgName.Trim(),
                        DateEntered = a.DateEntered,
                        GroupNumber = a.GroupNumber.Trim(),
                        JobTitle = a.JobTitle.Trim(),
                        FileNames = a.FileNames.Trim(),
                        AnalystType = a.AnalystType.ToString(),
                        Incumbents = a.Incumbents,
                    });

            //  Filter by Customer ID
            if (filter.CustomerID > 0)
            {
                rows = rows.Where(r => r.CustomerID == filter.CustomerID);
            }
            else
            {
                rows = rows.Where(r => r.CustomerID != null);
            }

            //  Filter by DOT Number
            if (!string.IsNullOrEmpty(filter.DOTNumberFrom))
            {
                if (string.IsNullOrEmpty(filter.DOTNumberTo))
                {
                    rows = rows.Where(r => r.DOTNumber == filter.DOTNumberFrom);
                }
                else
                {
                    rows = rows.Where(r => r.DOTNumber.CompareTo(filter.DOTNumberFrom) >= 0
                        && r.DOTNumber.CompareTo(filter.DOTNumberTo) <= 0);
                }
            }

            //  Filter by OrgName
            if (!string.IsNullOrEmpty(filter.OrgName))
            {
                rows = rows.Where(r => r.OrgName.StartsWith(filter.OrgName));
            }

            //  Filter by Group
            if (!string.IsNullOrEmpty(filter.GroupNumberFrom))
            {
                if (!string.IsNullOrEmpty(filter.GroupNumberTo))
                {
                    rows = rows.Where(r => r.GroupNumber == filter.GroupNumberFrom);
                }
                else
                {
                    rows = rows.Where(r => r.GroupNumber.CompareTo(filter.GroupNumberFrom) >= 0
                        && r.GroupNumber.CompareTo(filter.GroupNumberTo) <= 0);
                }
            }

            if (filter.Skip > 0)
            {
                rows = rows.Skip(filter.Skip);
            }
            if (filter.Take > 0)
            {
                rows = rows.Take(filter.Take);
            }
            else
            {
                rows = rows.Take(100);
            }

            return rows.Distinct();
        }
    }
}

I have a gridview in my user layer that uses a business layer method as its datasource, and I want the gridview to support paging and sorting. When I returned an Ienumerable from the method, it will bring back all of the data. If I use Take/Skip to bring back only a page's worth, the gridview doesn't realize that there are many pages of data.

When I change the Ienumerable to an IQueryable, the DataBind() fails because the data has already been disposed of. I think this problem has something to do with when the query actually executes and it may have to do with the filters I am applying to the query (see below).

User Layer Code:

grdSelectedQuestionaires.DataSource = assessment.FilteredAssessmentList(filter);
grdSelectedQuestionaires.DataBind(); <-- Fails - "Cannot access a disposed object"

Business Layer Code:

public IQueryable<Assessment> FilteredAssessmentList(AssessmentSearchFilter filter)
    {
        using (PAQcDataLayerDataContext dc = new PAQcDataLayerDataContext())
        {
            var rows = (
                    from a in dc.vPAQSummaries
                    select new Assessment()
                    {
                        PAQNumber = a.PAQNumber.Trim(),
                        CustomerID = (int)a.CustomerID,
                        Department = a.Department.Trim(),
                        CustomerName = a.CustomerName.Trim(),
                        DOTNumber = a.DOTNumber.Trim(),
                        OrgName = a.OrgName.Trim(),
                        DateEntered = a.DateEntered,
                        GroupNumber = a.GroupNumber.Trim(),
                        JobTitle = a.JobTitle.Trim(),
                        FileNames = a.FileNames.Trim(),
                        AnalystType = a.AnalystType.ToString(),
                        Incumbents = a.Incumbents,
                    });

            //  Filter by Customer ID
            if (filter.CustomerID > 0)
            {
                rows = rows.Where(r => r.CustomerID == filter.CustomerID);
            }
            else
            {
                rows = rows.Where(r => r.CustomerID != null);
            }

            //  Filter by DOT Number
            if (!string.IsNullOrEmpty(filter.DOTNumberFrom))
            {
                if (string.IsNullOrEmpty(filter.DOTNumberTo))
                {
                    rows = rows.Where(r => r.DOTNumber == filter.DOTNumberFrom);
                }
                else
                {
                    rows = rows.Where(r => r.DOTNumber.CompareTo(filter.DOTNumberFrom) >= 0
                        && r.DOTNumber.CompareTo(filter.DOTNumberTo) <= 0);
                }
            }

            //  Filter by OrgName
            if (!string.IsNullOrEmpty(filter.OrgName))
            {
                rows = rows.Where(r => r.OrgName.StartsWith(filter.OrgName));
            }

            //  Filter by Group
            if (!string.IsNullOrEmpty(filter.GroupNumberFrom))
            {
                if (!string.IsNullOrEmpty(filter.GroupNumberTo))
                {
                    rows = rows.Where(r => r.GroupNumber == filter.GroupNumberFrom);
                }
                else
                {
                    rows = rows.Where(r => r.GroupNumber.CompareTo(filter.GroupNumberFrom) >= 0
                        && r.GroupNumber.CompareTo(filter.GroupNumberTo) <= 0);
                }
            }

            if (filter.Skip > 0)
            {
                rows = rows.Skip(filter.Skip);
            }
            if (filter.Take > 0)
            {
                rows = rows.Take(filter.Take);
            }
            else
            {
                rows = rows.Take(100);
            }

            return rows.Distinct();
        }
    }
}

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

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

发布评论

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

评论(1

梦冥 2024-10-19 18:58:12

分页问题是由Using语句引起的。将其更改

using (PAQcDataLayerDataContext dc = new PAQcDataLayerDataContext())
{
...
}

为:

PAQcDataLayerDataContext dc = new PAQcDataLayerDataContext();

使分页工作。现在我必须弄清楚排序是如何工作的。

The paging problem was caused by the Using statement. Changing this:

using (PAQcDataLayerDataContext dc = new PAQcDataLayerDataContext())
{
...
}

to this:

PAQcDataLayerDataContext dc = new PAQcDataLayerDataContext();

Made paging work. Now I have to figure out how sorting works.

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