datatable按照某列排序【linq】

/// <summary>
/// 对datatable按照某一列进行linq排序,因为dataview自带的排序处理不了负数的排序【当成字符串处理的?待考究】
/// </summary>
/// <param name="tmpDt">需要排序的datatable</param>
/// <param name="sortId">排序的列序号,从0开始</param>
/// <returns></returns>
protected DataTable LinqSortDataTable(DataTable tmpDt,int sortId)
{
DataView dv = tmpDt.DefaultView;
var query = (from item in tmpDt.AsEnumerable()
orderby Convert.ToInt32(item[sortId])
select item);
//重新排序
dv = query.AsDataView();
return dv.ToTable();
}

你可能感兴趣的:(Datatable)