如何对IEnumerable <dictionary <string,object =“” >>结果集上的一个或多个键进行排序?

时间:2018-12-24 18:46:56

标签: c# dictionary json.net ienumerable

我们有一个视图,该视图对API进行ajax调用,该API返回类型为IEnumerable<Dictionary<string, object>>的结果集,并将该数据绑定到jQuery DataTable。每个Dictionary<string, object>基本上代表一个DataRow。如何根据一个或多个键并在服务器端指定的顺序对IEnumerable<Dictionary<string, object>>集合进行排序?

var sortOrderables = (JArray)_dtRequest["order"];
for (int i = 0; i < sortOrderables.Count; i++)
{
     var columnIdx = (int)sortOrderables[i]["column"];                
     var columnDir = (string)sortOrderables[i]["dir"];
     var column = (string)_dtRequest.SelectToken(string.Format("columns[{0}].data", columnIdx));    

     foreach(Dictionary<string, object> dataRow in _dataSet)
     {

     }
}

1 个答案:

答案 0 :(得分:-2)

此代码行得通吗?

            DataTable dt = new DataTable();

            Dictionary<string, DataTable> dict = dt.AsEnumerable()
                .GroupBy(x => x.Field<string>("Col A"), y => y)
                .ToDictionary(x => x.Key, y => y.CopyToDataTable());

            foreach (KeyValuePair<string, DataTable> key in dict.AsEnumerable())
            {
                int numberColumns = key.Value.Columns.Count;
                for (int i = numberColumns - 1; i >= 0; i--)
                {
                    key.Value.AsEnumerable().OrderBy(x => x[i]);
                }
            }