我有一个函数来执行返回我的SQL查询
List<Dictionary<string, string>>
第一个元素是字段,第二个元素是值。
在特定情况下,此列表具有以下值:
{ datetime, '01/23/2017 01:10:30' }
{ datetime, '01/23/2017 10:00:00' }
{ datetime, '01/23/2017 11:23:15' }
{ datetime, '01/20/2017 07:13:20' }
{ datetime, '01/20/2017 08:20:11' }
{ datetime, '01/21/2017 07:28:29' }
我需要将其转换为:
{ '01/23/2017', { '01/23/2017 01:10:30', ''01/23/2017 10:00:00', '01/23/2017 11:23:15' } }
{ '01/20/2017', { '01/20/2017 07:13:20', '01/20/2017 08:20:11' } }
{ '01/21/2017', { '01/21/2017 07:28:29' } }
我该如何处理?
答案 0 :(得分:3)
根据您提供的数据,这是一种方法。请注意,此代码是在LinqPad中编写和执行的,因此.Dump()
仅适用于此处:
var source = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>( "datetime", "01/23/2017 01:10:30") ,
new KeyValuePair<string, string>( "datetime", "01/23/2017 10:00:00" ),
new KeyValuePair<string, string>( "datetime", "01/23/2017 11:23:15" ),
new KeyValuePair<string, string>( "datetime", "01/20/2017 07:13:20" ),
new KeyValuePair<string, string>( "datetime", "01/20/2017 08:20:11" ),
new KeyValuePair<string, string>( "datetime", "01/21/2017 07:28:29" )
};
var list = source.Select (s => s.Value)
.GroupBy (s => DateTime.ParseExact(s, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture).Date)
.ToList();
list.Dump();
输出: