2017-01-23 80 views
-5

我不得不执行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

了'datetime'是不相关的,对不对? – Tatranskymedved

+0

正确的,它是由查询返回的字段的只是名字。 – msmosso

+0

从数据库的响应似乎很奇怪字典...为什么名单? – pimbrouwers

回答

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(); 

输出:

enter image description here