0

如何为网格视图执行排序操作。我已经完成了这个(下面的代码),但它抛出了以下错误。

错误:

 the type arguments for method 'System.Linq.Enumerable.OrderBy<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>,
     System.Func<TSource,TKey>, System.Collections.Generic.IComparer<TKey>)' 
    cannot be inferred from the usage.

代码:

gridview1.DataSource = (from bk in bookList
                                join ordr in bookOrders
                                on bk.BookID equals ordr.BookID
                                select new
                                {
                                    BookID = bk.BookID,
                                    BookNm = bk.BookNm,
                                    PaymentMode = ordr.PaymentMode
                                }).AsQueryable().OrderBy(e.SortExpression,e.SortDirection);
4

2 回答 2

1

您正在将基于字符串的排序机制(GridView.Sorting 事件)与 Linq(基于 lambda 表达式的排序)混合。

这篇文章:Handle GridView.OnSorting() and create sorting expression dynamic using LINQ does what you want

于 2013-10-19T17:30:05.500 回答
0

最后我做了以下

protected void gridview1_Sorting(object sender, GridViewSortEventArgs e)
        {
            int p = int.Parse(Session["p"].ToString());
            p++;
            Session["p"] = p;
            if (p % 2 == 0)
            {
                var A = from bk in bookList
                        join ordr in bookOrders
                        on bk.BookID equals ordr.BookID
                        select new
                        {
                            BookID = bk.BookID,
                            BookNm = bk.BookNm,
                            PaymentMode = ordr.PaymentMode
                        };


                gridview1.DataSource = A.OrderBy(x => x.BookID);
                gridview1.DataBind();

            }
            else
            {
                var A = from bk in bookList
                        join ordr in bookOrders
                        on bk.BookID equals ordr.BookID
                        select new
                        {
                            BookID = bk.BookID,
                            BookNm = bk.BookNm,
                            PaymentMode = ordr.PaymentMode
                        };


                gridview1.DataSource = A.OrderByDescending(x=>x.BookID);
                gridview1.DataBind();

            }



        }
于 2013-10-20T12:48:24.063 回答