对于我当前的项目,我们有几个表需要显示/更新/插入/删除/等。我正在使用GridView,因为它对我所涉及的数据最有意义。所以我使用LINQ从数据库中获取数据。到目前为止,非常简单的查询... GetAll和GetByID类型的东西 所以我的查询看起来像:
List<ds_Users_old> selectedUser = DataContext1.ds_Users_olds.ToList();
和
List<ds_Users_old> selectedUser = DataContext1.ds_Users_olds.Where(user => user.WorkEmail == email).ToList();
那种事。
我很容易将myUsers绑定到GridView并完成它。除此之外,我还需要通过任何列对GridView进行排序。 SortExpression =“FirstName”,“LastName”,“OfficePhone”,“UserID”等。myUsers.OrderByDescending(o => o.LastName).ToList();
工作正常,但我需要myUsers.OrderByDescending(o => SortExpression).ToList();
,但这不起作用。而且我不想为每个SortExpression创建不同的大小写。从我收集的内容来看,创造这种动态可能会变得非常混乱。
所以我的下一个想法是将我的LINQ结果转换为DAL中的DataTable,只返回DataTable而不是LINQ对象的泛型列表。 DataTables似乎更容易按动态字段排序。 dt.DefaultView.Sort = e.SortExpression + " " + mySortDirection;
等。但是在使用之前将我的LINQ结果转换为DataTable似乎有点笨重。
以下是我项目的简化版本:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
UsersGridView_BindGridViewData();
}
}
protected void UsersGridView_BindGridViewData()
{
List<ds_Users_old> allUsersList = UserClass.GetAllUsers();
DataTable allUsersDT = UserClass.GetAllUsersDT();
//UsersGridView.DataSource = allUsersList;
//UsersGridView.DataBind();
UsersGridView.DataSource = allUsersDT;
UsersGridView.DataBind();
Session["allUsersDT"] = allUsersDT;
}
protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = Session["testDT"] as DataTable;
if (dt != null)
{
if (e.SortExpression == "GroupName")
e.SortExpression = "GroupID";
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
UsersGridView.DataSource = Session["allUsersDT"];
UsersGridView.DataBind();
}
}
private string GetSortDirection(string column)
{
string sortDirection = "ASC";
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
这是我通过以下方式访问数据的类:
public static class UserClass
{
private static nsdc_supplyDataContext DataContext1 = new nsdc_supplyDataContext();
#region SELECTS
public static List<ds_Users_old> GetAllUsers()
{
List<ds_Users_old> selectedUser = DataContext1.ds_Users_olds.ToList();
return selectedUser;
}
public static DataTable GetAllUsersDT()
{
List<ds_Users_old> selectedUser = DataContext1.ds_Users_olds.ToList();
DataTable selectedUserDT = ConvertToDataTable(selectedUser);
return selectedUserDT;
}
public static List<ds_Users_old> GetUsersByWorkEmail(string email)
{
List<ds_Users_old> selectedUser = DataContext1.ds_Users_olds.Where(user => user.WorkEmail == email).ToList();
return selectedUser;
}
public static DataTable GetUsersByWorkEmailDT(string email)
{
List<ds_Users_old> selectedUsers = DataContext1.ds_Users_olds.Where(user => user.WorkEmail == email).ToList();
DataTable selectedUsersDT = ConvertToDataTable(selectedUsers);
return selectedUsersDT;
}
#endregion
public static DataTable ConvertToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
}
这看起来很混乱,并且觉得必须采用更优雅的方式。
答案 0 :(得分:1)
由于您使用对象列表作为数据源,因此您需要实现自己的排序逻辑,如下所示:
public sealed class GenericComparer<T> : IComparer<T>
{
public enum SortOrder
{
Ascending = 0,
Descending = 1
}
private string sortColumn;
private SortOrder sortingOrder;
public string SortColumn
{
get
{
return this.sortColumn;
}
}
public SortOrder SortingOrder
{
get
{
return this.sortingOrder;
}
}
public GenericComparer(string theSortColumn, SortOrder theSortingOrder)
{
this.sortColumn = theSortColumn;
this.sortingOrder = theSortingOrder;
}
public int Compare(T x, T y)
{
PropertyInfo thePropertyInfo = typeof(T).GetProperty(this.sortColumn);
IComparable object1 = (IComparable)thePropertyInfo.GetValue(x, null);
IComparable object2 = (IComparable)thePropertyInfo.GetValue(y, null);
if (this.sortingOrder == SortOrder.Ascending)
{
return object1.CompareTo(object2);
}
else
{
return object2.CompareTo(object1);
}
}
}
现在,在对象列表中调用.Sort()
方法时,传递此辅助类的新实例(将要排序的列表属性和要排序的方向传递给它 - 上升或下降)。
由于上面的比较器逻辑使用泛型,您可以传递您想要排序的任何类型(即int
,DateTime
,甚至是域对象。)