Gridview排序不能与Linq一起使用SQL

时间:2012-07-10 20:54:48

标签: asp.net linq entity-framework sorting gridview

我在gridview上排序时出现以下错误。 "数据源不支持服务器端数据分页。"我没有使用obejctdatasource和linqdatasource。我发现堆栈溢出的每个示例或解决方案都使用obejctdatasource。 [例如:http://www.devtoolshed.com/content/gridview-objectdatasource-linq-paging-and-sorting]

以下是我的代码。如何修复代码中的排序错误?我可以在不使用objectdatasource的情况下修复它。


public List<SearchLoanViewItem> GetSearchresult(EmpowerEntities empower)
        {
            var searchresult = (from m in dbo.MTGTERMS
                                join p in dbo.PROPINFOes
                                    on m.LNKEY equals p.LNKEY
                                join b in dbo.BORRINFOes
                                    on m.LNKEY equals b.LNKEY
                                join d in dbo.DBLOCKS
                                    on m.LNKEY equals d.LNKEY
                                join u in dbo.UCODES3
                                    on m.LNKEY equals u.LNKEY
                                join c in dbo.CODES
                                    on m.LNKEY equals c.LNKEY
                                where b.WHICHBORR == 1
                                      && (c.APPSTATS == null ? "" : c.APPSTATS) != "DEL"
                                select new SearchLoanViewItem
                                           {
                                               lnkey = m.LNKEY,
                                               prop_addr = p.PROP_ADDR,
                                               networklocked = d.NETWORKLOCKED == "Y" ? "Yes" : "No",
                                               username = (d.USERNAME ?? ""),
                                               mailbox = d.MAILBOX,
                                               borr_name = b.BORR_NAME,
                                               ssn = b.BORR_SSN,
                                               phone = b.BORR_PHONE,
                                               employee_loan = (u.CODE67N ?? -1)
                                           }).Take(5).ToList();
            return searchresult;
        }


        public void ShowSearchResults()
        {
            try
            {
                var empower = new EmpowerEntities();
                var searchresult = GetSearchresult(empower);                

                gdvSearchResult.DataSource = searchresult;
                gdvSearchResult.DataBind();
            }
            catch (Exception ex)
            {
                Logger.log("ucSearchLoan", ex.Message, Logger.ERROR);
            }
        }


 protected void Paging_gdvSearchResult(object source, GridViewPageEventArgs e)
        {  
            gdvSearchResult.PageIndex = e.NewPageIndex;
            ShowSearchResults();
        }

protected void gdvSearchResult_SortCommand(object source, GridViewSortEventArgs e)
        {
            try
            {
                var empower = new EmpowerEntities();
                var searchresult = GetSearchresult(empower);

                if (SearchSortType == Constants.ASC)
                    SearchSortType = Constants.DESC;
                else
                    SearchSortType = Constants.ASC;

                SeachSortField = e.SortExpression.ToString();

                if (SeachSortField.Equals(Constants.ASC))
                    gdvSearchResult.DataSource = searchresult.AsEnumerable().OrderBy(a => a.GetType().GetProperty(SeachSortField).GetValue(a, null));
                else
                    gdvSearchResult.DataSource = searchresult.AsEnumerable().OrderByDescending(a => a.GetType().GetProperty(SeachSortField).GetValue(a, null));

                gdvSearchResult.DataBind();        

            }

and my gridview looks like below.

    <asp:GridView runat="server" ID="gdvSearchResult" 
             AutoGenerateColumns="false" OnRowCommand="gdvSearchResult_RowCommand" OnRowCreated="gdvSearchResult_RowCreated" PageSize="2" AllowPaging="true"      AllowSorting="true" OnPageIndexChanging="Paging_gdvSearchResult" OnSorting="gdvSearchResult_SortCommand">

1 个答案:

答案 0 :(得分:1)

我添加了投射[Cast<object>().ToList()部分]

时修复了问题
 if (SeachSortField.Equals(Constants.ASC))
                    gdvSearchResult.DataSource = searchresult.AsEnumerable().OrderBy(a => a.GetType().GetProperty(SeachSortField).GetValue(a, null)).Cast<object>().ToList();
                else
                    gdvSearchResult.DataSource = searchresult.AsEnumerable().OrderByDescending(a => a.GetType().GetProperty(SeachSortField).GetValue(a, null)).Cast<object>().ToList();