使用LinQ给DataTable排序

string OrderRoom = "desc";

DataTable dt = new FloorCateRoomSystem().getBieShuRoomListSystem(301).Tables[0];

EnumerableRowCollection<DataRow> roomEnumer;
if (OrderRoom == "desc")
{
    var selectedRows = from r in dt.AsEnumerable() orderby r.Field<int>("alias") descending select r;
    roomEnumer = selectedRows;
}
else {
    var selectedRows = from r in dt.AsEnumerable() orderby r.Field<int>("alias") select r;
    roomEnumer = selectedRows;
}
foreach (var item in roomEnumer)
{
    Response.Write(item["name"].ToString() + ", " + item["alias"].ToString() + "</br>");
}

你可能感兴趣的:(Datatable)