LinqSql Sorting Asp.net

本文关键字:net Asp Sorting LinqSql | 更新日期: 2023-09-27 18:30:52

SessionIDs ids = (SessionIDs)Session["IDs"];
BSDataContext ct = new BSDataContext();
var customers = from cust in ct.tblCustomers
                where cust.AccountID == ids.accountID
                join mem in ct.tblCustomerMemberships on cust.CustomerID equals mem.CustomerID
                orderby drpFilter.SelectedValue descending
                select new { cust.CustomerID, cust.Mobile, cust.BusinessPhone, cust.Code, cust.Email, cust.HomePhone, mem.Membership, Name = cust.FirstName + cust.LastName };
grdCustomer.DataSource = customers;
grdCustomer.DataBind();

仍然排序未执行。

如果我写cust.Code而不是drp.selectedvalue那么排序就完成了!为什么?

LinqSql Sorting Asp.net

您必须

检查用户想要对哪一列进行排序并相应地对其进行排序的条件if

var customers = from cust in ct.tblCustomers
                        where cust.AccountID == ids.accountID
                        join mem in ct.tblCustomerMemberships
                        on cust.CustomerID equals mem.CustomerID
                        select new 
                              { cust.CustomerID, 
                                cust.Mobile, 
                                cust.BusinessPhone, 
                                cust.Code, 
                                cust.Email, 
                                cust.HomePhone, 
                                mem.Membership, 
                               Name = cust.FirstName + cust.LastName 
                             };
if(drpFilter.SelectedValue == "Code")
   customers = customers.OrderByDescending(x=>x.Code);