我有一个非常大的数据表,其中包含名为ID,Date的列 其中有12000条记录。所以记录样本是:
1001,02/06/2016 12:21:20monday
1002,01/06/2016 12:21:20monday
1001,03/06/2016 12:21:20monday
1004,03/06/2016 12:21:20monday
1005,04/06/2016 12:21:20monday
1001,01/06/2016 12:21:20monday
1004,02/06/2016 12:21:20monday
1006,06/06/2016 12:21:20monday
1005,05/06/2016 12:21:20monday
1002,07/06/2016 12:21:20monday
1003,08/06/2016 12:21:20monday
1001,02/06/2016 12:21:20monday
1004,01/06/2016 12:21:20monday
1005,04/06/2016 12:21:20monday
1002,03/06/2016 12:21:20monday
1003,02/06/2016 12:21:20monday
1006,06/06/2016 12:21:20monday
1004,02/06/2016 12:21:20monday
1005,02/06/2016 12:21:20monday
1002,02/06/2016 12:21:20monday
所以我想制作一个包含以下结构的列表
List<KeyValuePair<string, string[]>> dicc = new List<KeyValuePair<string, string[]>>();
第一项包含ID字段,第二项是字符串数组,其中包含日期字段,如下所示
1001
02/06/2016 12:21:20monday
01/06/2016 12:21:20monday
03/06/2016 12:21:20monday
03/06/2016 12:21:20monday
04/06/2016 12:21:20monday
01/06/2016 12:21:20monday
1002
02/06/2016 12:21:20monday
06/06/2016 12:21:20monday
05/06/2016 12:21:20monday
07/06/2016 12:21:20monday
08/06/2016 12:21:20monday
1003
02/06/2016 12:21:20monday
01/06/2016 12:21:20monday
04/06/2016 12:21:20monday
03/06/2016 12:21:20monday
02/06/2016 12:21:20monday
那我怎么能这样做。请帮助我
答案 0 :(得分:1)
您可以使用LINQ查询选择KeyValuePairs
的{{1}}
DataTable
使用List<KeyValuePair<string, string[]>> dicc = dataTable.AsEnumerable()
.Select(Row => Row["ID"]).Distinct()
.Select(Id => new KeyValuePair<string, string[]>(
Id.ToString(),
dataTable.AsEnumerable()
.Where(Row => Row["ID"].ToString() == Id.ToString())
//.OrderBy(Row => Row["Date"])
.Select(Row => Row["Date"].ToString())
.ToArray()))
.ToList();
DataTable
返回一个列表
- 密钥:&#34; 1&#34; 值:&#34;日期1&#34;,&#34;日期2&#34;,&#34;日期3&#34;
- 密钥:&#34; 2&#34; 值:&#34;日期4&#34;,&#34;日期5&#34;
醇>